user32: Add a driver entry point for UpdateLayeredWindow.
[wine] / dlls / user32 / exticon.c
1 /*
2  *      icon extracting
3  *
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
7  *
8  * Copyright 2000 Juergen Schmied
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26
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
34
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winnls.h"
42 #include "user_private.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(icon);
46
47 #include "pshpack1.h"
48
49 typedef struct
50 {
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;
60
61 typedef struct
62 {
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;
68
69 typedef struct
70 {
71     WORD offset;
72     WORD length;
73     WORD flags;
74     WORD id;
75     WORD handle;
76     WORD usage;
77 } NE_NAMEINFO;
78
79 typedef struct
80 {
81     WORD  type_id;
82     WORD  count;
83     DWORD resloader;
84 } NE_TYPEINFO;
85
86 #define NE_RSCTYPE_ICON        0x8003
87 #define NE_RSCTYPE_GROUP_ICON  0x800e
88
89 #include "poppack.h"
90
91 #if 0
92 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
93 {
94         TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
95         TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
96         TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
97         entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
98 }
99 static void dumpIcoDir ( LPicoICONDIR entry )
100 {
101         TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
102 }
103 #endif
104
105 /**********************************************************************
106  *  find_entry_by_id
107  *
108  * Find an entry by id in a resource directory
109  * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
110  */
111 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
112                                                          WORD id, const void *root )
113 {
114     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
115     int min, max, pos;
116
117     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
118     min = dir->NumberOfNamedEntries;
119     max = min + dir->NumberOfIdEntries - 1;
120     while (min <= max)
121     {
122         pos = (min + max) / 2;
123         if (entry[pos].u1.s2.Id == id)
124             return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
125         if (entry[pos].u1.s2.Id > id) max = pos - 1;
126         else min = pos + 1;
127     }
128     return NULL;
129 }
130
131 /**********************************************************************
132  *  find_entry_default
133  *
134  * Find a default entry in a resource directory
135  * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
136  */
137 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
138                                                            const void *root )
139 {
140     const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
141     entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
142     return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
143 }
144
145 /*************************************************************************
146  *                              USER32_GetResourceTable
147  */
148 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
149 {
150         IMAGE_DOS_HEADER        * mz_header;
151
152         TRACE("%p %p\n", peimage, retptr);
153
154         *retptr = NULL;
155
156         mz_header = (IMAGE_DOS_HEADER*) peimage;
157
158         if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
159         {
160           if (mz_header->e_cblp == 1)   /* .ICO file ? */
161           {
162             *retptr = (LPBYTE)-1;       /* ICONHEADER.idType, must be 1 */
163             return 1;
164           }
165           else
166             return 0; /* failed */
167         }
168         if (mz_header->e_lfanew >= pesize) {
169             return 0; /* failed, happens with PKZIP DOS Exes for instance. */
170         }
171         if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
172           return IMAGE_NT_SIGNATURE;
173
174         if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
175         {
176           IMAGE_OS2_HEADER      * ne_header;
177
178           ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
179
180           if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
181             return 0;
182
183           if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
184             *retptr = (LPBYTE)-1;
185           else
186             *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
187
188           return IMAGE_OS2_SIGNATURE;
189         }
190         return 0; /* failed */
191 }
192 /*************************************************************************
193  *                      USER32_LoadResource
194  */
195 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
196 {
197         TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
198
199         *uSize = (DWORD)pNInfo->length << sizeShift;
200         return peimage + ((DWORD)pNInfo->offset << sizeShift);
201 }
202
203 /*************************************************************************
204  *                      ICO_LoadIcon
205  */
206 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
207 {
208         TRACE("%p %p\n", peimage, lpiIDE);
209
210         *uSize = lpiIDE->dwBytesInRes;
211         return peimage + lpiIDE->dwImageOffset;
212 }
213
214 /*************************************************************************
215  *                      ICO_GetIconDirectory
216  *
217  * Reads .ico file and build phony ICONDIR struct
218  */
219 #define HEADER_SIZE             (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
220 #define HEADER_SIZE_FILE        (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
221
222 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
223 {
224         CURSORICONDIR   * lpcid;        /* icon resource in resource-dir format */
225         CURSORICONDIR   * lpID;         /* icon resource in resource format */
226         int             i;
227
228         TRACE("%p %p\n", peimage, lplpiID);
229
230         lpcid = (CURSORICONDIR*)peimage;
231
232         if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
233           return 0;
234
235         /* allocate the phony ICONDIR structure */
236         *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
237         if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
238         {
239           /* copy the header */
240           lpID->idReserved = lpcid->idReserved;
241           lpID->idType = lpcid->idType;
242           lpID->idCount = lpcid->idCount;
243
244           /* copy the entries */
245           for( i=0; i < lpcid->idCount; i++ )
246           {
247             memcpy(&lpID->idEntries[i], &lpcid->idEntries[i], sizeof(CURSORICONDIRENTRY) - 2);
248             lpID->idEntries[i].wResId = i;
249           }
250
251           *lplpiID = (LPicoICONDIR)peimage;
252           return (BYTE *)lpID;
253         }
254         return 0;
255 }
256
257 /*************************************************************************
258  *      ICO_ExtractIconExW              [internal]
259  *
260  * NOTES
261  *  nIcons = 0: returns number of Icons in file
262  *
263  * returns
264  *  invalid file: -1
265  *  failure:0;
266  *  success: number of icons in file (nIcons = 0) or nr of icons retrieved
267  */
268 static UINT ICO_ExtractIconExW(
269         LPCWSTR lpszExeFileName,
270         HICON * RetPtr,
271         INT nIconIndex,
272         UINT nIcons,
273         UINT cxDesired,
274         UINT cyDesired,
275         UINT *pIconId,
276         UINT flags)
277 {
278         UINT            ret = 0;
279         UINT            cx1, cx2, cy1, cy2;
280         LPBYTE          pData;
281         DWORD           sig;
282         HANDLE          hFile;
283         UINT16          iconDirCount = 0,iconCount = 0;
284         LPBYTE          peimage;
285         HANDLE          fmapping;
286         DWORD           fsizeh,fsizel;
287         WCHAR           szExePath[MAX_PATH];
288         DWORD           dwSearchReturn;
289
290         TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
291
292         dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, sizeof(szExePath) / sizeof(szExePath[0]), szExePath, NULL);
293         if ((dwSearchReturn == 0) || (dwSearchReturn > sizeof(szExePath) / sizeof(szExePath[0])))
294         {
295             WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
296             return -1;
297         }
298
299         hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
300         if (hFile == INVALID_HANDLE_VALUE) return ret;
301         fsizel = GetFileSize(hFile,&fsizeh);
302
303         /* Map the file */
304         fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
305         CloseHandle(hFile);
306         if (!fmapping)
307         {
308           WARN("CreateFileMapping error %d\n", GetLastError() );
309           return 0xFFFFFFFF;
310         }
311
312         if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
313         {
314           WARN("MapViewOfFile error %d\n", GetLastError() );
315           CloseHandle(fmapping);
316           return 0xFFFFFFFF;
317         }
318         CloseHandle(fmapping);
319
320         cx1 = LOWORD(cxDesired);
321         cx2 = HIWORD(cxDesired);
322         cy1 = LOWORD(cyDesired);
323         cy2 = HIWORD(cyDesired);
324
325         if (pIconId) /* Invalidate first icon identifier */
326                 *pIconId = 0xFFFFFFFF;
327
328         if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
329           pIconId = (UINT*)RetPtr;
330
331         sig = USER32_GetResourceTable(peimage, fsizel, &pData);
332
333 /* ico file or NE exe/dll*/
334         if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
335         {
336           BYTE          *pCIDir = 0;
337           NE_TYPEINFO   *pTInfo = (NE_TYPEINFO*)(pData + 2);
338           NE_NAMEINFO   *pIconStorage = NULL;
339           NE_NAMEINFO   *pIconDir = NULL;
340           LPicoICONDIR  lpiID = NULL;
341           ULONG         uSize = 0;
342
343           TRACE("-- OS2/icon Signature (0x%08x)\n", sig);
344
345           if (pData == (BYTE*)-1)
346           {
347             pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize);     /* check for .ICO file */
348             if (pCIDir)
349             {
350               iconDirCount = 1; iconCount = lpiID->idCount;
351               TRACE("-- icon found %p 0x%08x 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
352             }
353           }
354           else while (pTInfo->type_id && !(pIconStorage && pIconDir))
355           {
356             if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON)       /* find icon directory and icon repository */
357             {
358               iconDirCount = pTInfo->count;
359               pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
360               TRACE("\tfound directory - %i icon families\n", iconDirCount);
361             }
362             if (pTInfo->type_id == NE_RSCTYPE_ICON)
363             {
364               iconCount = pTInfo->count;
365               pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
366               TRACE("\ttotal icons - %i\n", iconCount);
367             }
368             pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
369           }
370
371           if ((pIconStorage && pIconDir) || lpiID)        /* load resources and create icons */
372           {
373             if (nIcons == 0)
374             {
375               ret = iconDirCount;
376               if (lpiID)        /* *.ico file, deallocate heap pointer*/
377                 HeapFree(GetProcessHeap(), 0, pCIDir);
378             }
379             else if (nIconIndex < iconDirCount)
380             {
381               UINT16   i, icon;
382               if (nIcons > iconDirCount - nIconIndex)
383                 nIcons = iconDirCount - nIconIndex;
384
385               for (i = 0; i < nIcons; i++)
386               {
387                 /* .ICO files have only one icon directory */
388                 if (lpiID == NULL)      /* not *.ico */
389                   pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
390                 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
391                 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE,  cx2, cy2, flags);
392               }
393               if (lpiID)        /* *.ico file, deallocate heap pointer*/
394                 HeapFree(GetProcessHeap(), 0, pCIDir);
395
396               for (icon = 0; icon < nIcons; icon++)
397               {
398                 pCIDir = NULL;
399                 if (lpiID)
400                   pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
401                 else
402                   for (i = 0; i < iconCount; i++)
403                     if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
404                       pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
405
406                 if (pCIDir)
407                 {
408                   RetPtr[icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
409                                                                  cx1, cy1, flags);
410                   if (cx2 && cy2)
411                       RetPtr[++icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
412                                                                        cx2, cy2, flags);
413                 }
414                 else
415                   RetPtr[icon] = 0;
416               }
417               ret = icon;       /* return number of retrieved icons */
418             }
419           }
420         }
421 /* end ico file */
422
423 /* exe/dll */
424         else if( sig == IMAGE_NT_SIGNATURE )
425         {
426           LPBYTE                idata,igdata;
427           PIMAGE_DOS_HEADER     dheader;
428           PIMAGE_NT_HEADERS     pe_header;
429           PIMAGE_SECTION_HEADER pe_sections;
430           const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
431           const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
432           const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
433           UINT  i, j;
434
435           dheader = (PIMAGE_DOS_HEADER)peimage;
436           pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);     /* it is a pe header, USER32_GetResourceTable checked that */
437           pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER)
438                                                 + pe_header->FileHeader.SizeOfOptionalHeader);
439           rootresdir = NULL;
440
441           /* search for the root resource directory */
442           for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
443           {
444             if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
445               continue;
446             if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
447               FIXME("File %s too short (section is at %d bytes, real size is %d)\n",
448                       debugstr_w(lpszExeFileName),
449                       pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
450                       fsizel
451               );
452               goto end;
453             }
454             /* FIXME: doesn't work when the resources are not in a separate section */
455             if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
456             {
457               rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
458               break;
459             }
460           }
461
462           if (!rootresdir)
463           {
464             WARN("haven't found section for resource directory.\n");
465             goto end;           /* failure */
466           }
467
468           /* search for the group icon directory */
469           if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
470           {
471             WARN("No Icongroupresourcedirectory!\n");
472             goto end;           /* failure */
473           }
474           iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
475
476           /* only number of icons requested */
477           if( !pIconId )
478           {
479             ret = iconDirCount;
480             goto end;           /* success */
481           }
482
483           if( nIconIndex < 0 )
484           {
485             /* search resource id */
486             int n = 0;
487             int iId = abs(nIconIndex);
488             const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
489
490             while(n<iconDirCount && xprdeTmp)
491             {
492               if(xprdeTmp->u1.s2.Id ==  iId)
493               {
494                   nIconIndex = n;
495                   break;
496               }
497               n++;
498               xprdeTmp++;
499             }
500             if (nIconIndex < 0)
501             {
502               WARN("resource id %d not found\n", iId);
503               goto end;         /* failure */
504             }
505           }
506           else
507           {
508             /* check nIconIndex to be in range */
509             if (nIconIndex >= iconDirCount)
510             {
511               WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
512               goto end;         /* failure */
513             }
514           }
515
516           /* assure we don't get too much */
517           if( nIcons > iconDirCount - nIconIndex )
518             nIcons = iconDirCount - nIconIndex;
519
520           /* starting from specified index */
521           xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
522
523           for (i=0; i < nIcons; i++,xresent++)
524           {
525             const IMAGE_RESOURCE_DIRECTORY *resdir;
526
527             /* go down this resource entry, name */
528             resdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)rootresdir + xresent->u2.s3.OffsetToDirectory);
529
530             /* default language (0) */
531             resdir = find_entry_default(resdir,rootresdir);
532             igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
533
534             /* lookup address in mapped image for virtual address */
535             igdata = NULL;
536
537             for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
538             {
539               if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
540                 continue;
541               if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
542                 continue;
543
544               if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
545                 FIXME("overflow in PE lookup (%s has len %d, have offset %d), short file?\n", debugstr_w(lpszExeFileName), fsizel,
546                            igdataent->OffsetToData - pe_sections[j].VirtualAddress + pe_sections[j].PointerToRawData + igdataent->Size);
547                 goto end; /* failure */
548               }
549               igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
550             }
551
552             if (!igdata)
553             {
554               FIXME("no matching real address for icongroup!\n");
555               goto end; /* failure */
556             }
557             pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
558             if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
559           }
560
561           if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
562           {
563             WARN("No Iconresourcedirectory!\n");
564             goto end;           /* failure */
565           }
566
567           for (i=0; i<nIcons; i++)
568           {
569             const IMAGE_RESOURCE_DIRECTORY *xresdir;
570             xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
571             if( !xresdir )
572             {
573               WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
574               RetPtr[i]=0;
575               continue;
576             }
577             xresdir = find_entry_default(xresdir, rootresdir);
578             idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
579             idata = NULL;
580
581             /* map virtual to address in image */
582             for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
583             {
584               if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
585                 continue;
586               if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
587                 continue;
588               idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
589             }
590             if (!idata)
591             {
592               WARN("no matching real address found for icondata!\n");
593               RetPtr[i]=0;
594               continue;
595             }
596             RetPtr[i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx1, cy1, flags);
597             if (cx2 && cy2)
598                 RetPtr[++i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx2, cy2, flags);
599           }
600           ret = i;      /* return number of retrieved icons */
601         }                       /* if(sig == IMAGE_NT_SIGNATURE) */
602
603 end:
604         UnmapViewOfFile(peimage);       /* success */
605         return ret;
606 }
607
608 /***********************************************************************
609  *           PrivateExtractIconsW                       [USER32.@]
610  *
611  * NOTES
612  *  If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
613  *  returned, with the LOWORD size icon first and the HIWORD size icon
614  *  second.
615  *  Also the Windows equivalent does extract icons in a strange way if
616  *  nIndex is negative. Our implementation treats a negative nIndex as
617  *  looking for that resource identifier for the first icon to retrieve.
618  *
619  * FIXME:
620  *  should also support 16 bit EXE + DLLs, cursor and animated cursor as
621  *  well as bitmap files.
622  */
623
624 UINT WINAPI PrivateExtractIconsW (
625         LPCWSTR lpwstrFile,
626         int nIndex,
627         int sizeX,
628         int sizeY,
629         HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
630         UINT* pIconId,  /* [out] pointer to array of nIcons icon identifiers or NULL */
631         UINT nIcons,    /* [in] number of icons to retrieve */
632         UINT flags )    /* [in] LR_* flags used by LoadImage */
633 {
634         TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
635               debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
636
637         if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
638         {
639           WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
640         }
641         return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
642 }
643
644 /***********************************************************************
645  *           PrivateExtractIconsA                       [USER32.@]
646  */
647
648 UINT WINAPI PrivateExtractIconsA (
649         LPCSTR lpstrFile,
650         int nIndex,
651         int sizeX,
652         int sizeY,
653         HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
654         UINT* piconid,  /* [out] pointer to array of nIcons icon identifiers or NULL */
655         UINT nIcons,    /* [in] number of icons to retrieve */
656         UINT flags )    /* [in] LR_* flags used by LoadImage */
657 {
658     UINT ret;
659     INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
660     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
661
662     MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
663     ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
664
665     HeapFree(GetProcessHeap(), 0, lpwstrFile);
666     return ret;
667 }
668
669 /***********************************************************************
670  *           PrivateExtractIconExW                      [USER32.@]
671  * NOTES
672  *  if nIndex == -1 it returns the number of icons in any case !!!
673  */
674 UINT WINAPI PrivateExtractIconExW (
675         LPCWSTR lpwstrFile,
676         int nIndex,
677         HICON * phIconLarge,
678         HICON * phIconSmall,
679         UINT nIcons )
680 {
681         DWORD cyicon, cysmicon, cxicon, cxsmicon;
682         UINT ret = 0;
683
684         TRACE("%s %d %p %p %d\n",
685         debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
686
687         if (nIndex == -1)
688           /* get the number of icons */
689           return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
690
691         if (nIcons == 1 && phIconSmall && phIconLarge)
692         {
693           HICON hIcon[2];
694           cxicon = GetSystemMetrics(SM_CXICON);
695           cyicon = GetSystemMetrics(SM_CYICON);
696           cxsmicon = GetSystemMetrics(SM_CXSMICON);
697           cysmicon = GetSystemMetrics(SM_CYSMICON);
698
699           ret = ICO_ExtractIconExW(lpwstrFile, hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
700                                    cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
701           *phIconLarge = hIcon[0];
702           *phIconSmall = hIcon[1];
703           return ret;
704         }
705
706         if (phIconSmall)
707         {
708           /* extract n small icons */
709           cxsmicon = GetSystemMetrics(SM_CXSMICON);
710           cysmicon = GetSystemMetrics(SM_CYSMICON);
711           ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
712                                    cysmicon, NULL, LR_DEFAULTCOLOR);
713         }
714        if (phIconLarge)
715         {
716           /* extract n large icons */
717           cxicon = GetSystemMetrics(SM_CXICON);
718           cyicon = GetSystemMetrics(SM_CYICON);
719          ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
720                                    cyicon, NULL, LR_DEFAULTCOLOR);
721         }
722         return ret;
723 }
724
725 /***********************************************************************
726  *           PrivateExtractIconExA                      [USER32.@]
727  */
728 UINT WINAPI PrivateExtractIconExA (
729         LPCSTR lpstrFile,
730         int nIndex,
731         HICON * phIconLarge,
732         HICON * phIconSmall,
733         UINT nIcons )
734 {
735         UINT ret;
736         INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
737         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
738
739         TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
740
741         MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
742         ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
743         HeapFree(GetProcessHeap(), 0, lpwstrFile);
744         return ret;
745 }