d3drm: Just use RGBA_MAKE.
[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 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
220 {
221         CURSORICONDIR   * lpcid;        /* icon resource in resource-dir format */
222         CURSORICONDIR   * lpID;         /* icon resource in resource format */
223         int             i;
224
225         TRACE("%p %p\n", peimage, lplpiID);
226
227         lpcid = (CURSORICONDIR*)peimage;
228
229         if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
230           return 0;
231
232         /* allocate the phony ICONDIR structure */
233         *uSize = FIELD_OFFSET(CURSORICONDIR, idEntries[lpcid->idCount]);
234         if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
235         {
236           /* copy the header */
237           lpID->idReserved = lpcid->idReserved;
238           lpID->idType = lpcid->idType;
239           lpID->idCount = lpcid->idCount;
240
241           /* copy the entries */
242           for( i=0; i < lpcid->idCount; i++ )
243           {
244             memcpy(&lpID->idEntries[i], &lpcid->idEntries[i], sizeof(CURSORICONDIRENTRY) - 2);
245             lpID->idEntries[i].wResId = i;
246           }
247
248           *lplpiID = (LPicoICONDIR)peimage;
249           return (BYTE *)lpID;
250         }
251         return 0;
252 }
253
254 /*************************************************************************
255  *      ICO_ExtractIconExW              [internal]
256  *
257  * NOTES
258  *  nIcons = 0: returns number of Icons in file
259  *
260  * returns
261  *  invalid file: -1
262  *  failure:0;
263  *  success: number of icons in file (nIcons = 0) or nr of icons retrieved
264  */
265 static UINT ICO_ExtractIconExW(
266         LPCWSTR lpszExeFileName,
267         HICON * RetPtr,
268         INT nIconIndex,
269         UINT nIcons,
270         UINT cxDesired,
271         UINT cyDesired,
272         UINT *pIconId,
273         UINT flags)
274 {
275         UINT            ret = 0;
276         UINT            cx1, cx2, cy1, cy2;
277         LPBYTE          pData;
278         DWORD           sig;
279         HANDLE          hFile;
280         UINT16          iconDirCount = 0,iconCount = 0;
281         LPBYTE          peimage;
282         HANDLE          fmapping;
283         DWORD           fsizeh,fsizel;
284         WCHAR           szExePath[MAX_PATH];
285         DWORD           dwSearchReturn;
286
287         TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
288
289         dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, sizeof(szExePath) / sizeof(szExePath[0]), szExePath, NULL);
290         if ((dwSearchReturn == 0) || (dwSearchReturn > sizeof(szExePath) / sizeof(szExePath[0])))
291         {
292             WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
293             return -1;
294         }
295
296         hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
297         if (hFile == INVALID_HANDLE_VALUE) return ret;
298         fsizel = GetFileSize(hFile,&fsizeh);
299
300         /* Map the file */
301         fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
302         CloseHandle(hFile);
303         if (!fmapping)
304         {
305           WARN("CreateFileMapping error %d\n", GetLastError() );
306           return 0xFFFFFFFF;
307         }
308
309         if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
310         {
311           WARN("MapViewOfFile error %d\n", GetLastError() );
312           CloseHandle(fmapping);
313           return 0xFFFFFFFF;
314         }
315         CloseHandle(fmapping);
316
317         cx1 = LOWORD(cxDesired);
318         cx2 = HIWORD(cxDesired);
319         cy1 = LOWORD(cyDesired);
320         cy2 = HIWORD(cyDesired);
321
322         if (pIconId) /* Invalidate first icon identifier */
323                 *pIconId = 0xFFFFFFFF;
324
325         if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
326           pIconId = (UINT*)RetPtr;
327
328         sig = USER32_GetResourceTable(peimage, fsizel, &pData);
329
330 /* ico file or NE exe/dll*/
331         if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
332         {
333           BYTE          *pCIDir = 0;
334           NE_TYPEINFO   *pTInfo = (NE_TYPEINFO*)(pData + 2);
335           NE_NAMEINFO   *pIconStorage = NULL;
336           NE_NAMEINFO   *pIconDir = NULL;
337           LPicoICONDIR  lpiID = NULL;
338           ULONG         uSize = 0;
339
340           TRACE("-- OS2/icon Signature (0x%08x)\n", sig);
341
342           if (pData == (BYTE*)-1)
343           {
344             pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize);     /* check for .ICO file */
345             if (pCIDir)
346             {
347               iconDirCount = 1; iconCount = lpiID->idCount;
348               TRACE("-- icon found %p 0x%08x 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
349             }
350           }
351           else while (pTInfo->type_id && !(pIconStorage && pIconDir))
352           {
353             if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON)       /* find icon directory and icon repository */
354             {
355               iconDirCount = pTInfo->count;
356               pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
357               TRACE("\tfound directory - %i icon families\n", iconDirCount);
358             }
359             if (pTInfo->type_id == NE_RSCTYPE_ICON)
360             {
361               iconCount = pTInfo->count;
362               pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
363               TRACE("\ttotal icons - %i\n", iconCount);
364             }
365             pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
366           }
367
368           if ((pIconStorage && pIconDir) || lpiID)        /* load resources and create icons */
369           {
370             if (nIcons == 0)
371             {
372               ret = iconDirCount;
373               if (lpiID)        /* *.ico file, deallocate heap pointer*/
374                 HeapFree(GetProcessHeap(), 0, pCIDir);
375             }
376             else if (nIconIndex < iconDirCount)
377             {
378               UINT16   i, icon;
379               if (nIcons > iconDirCount - nIconIndex)
380                 nIcons = iconDirCount - nIconIndex;
381
382               for (i = 0; i < nIcons; i++)
383               {
384                 /* .ICO files have only one icon directory */
385                 if (lpiID == NULL)      /* not *.ico */
386                   pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
387                 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
388                 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE,  cx2, cy2, flags);
389               }
390               if (lpiID)        /* *.ico file, deallocate heap pointer*/
391                 HeapFree(GetProcessHeap(), 0, pCIDir);
392
393               for (icon = 0; icon < nIcons; icon++)
394               {
395                 pCIDir = NULL;
396                 if (lpiID)
397                   pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
398                 else
399                   for (i = 0; i < iconCount; i++)
400                     if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
401                       pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
402
403                 if (pCIDir)
404                 {
405                   RetPtr[icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
406                                                                  cx1, cy1, flags);
407                   if (cx2 && cy2)
408                       RetPtr[++icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
409                                                                        cx2, cy2, flags);
410                 }
411                 else
412                   RetPtr[icon] = 0;
413               }
414               ret = icon;       /* return number of retrieved icons */
415             }
416           }
417         }
418 /* end ico file */
419
420 /* exe/dll */
421         else if( sig == IMAGE_NT_SIGNATURE )
422         {
423           LPBYTE                idata,igdata;
424           PIMAGE_DOS_HEADER     dheader;
425           PIMAGE_NT_HEADERS     pe_header;
426           PIMAGE_SECTION_HEADER pe_sections;
427           const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
428           const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
429           const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
430           UINT  i, j;
431
432           dheader = (PIMAGE_DOS_HEADER)peimage;
433           pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);     /* it is a pe header, USER32_GetResourceTable checked that */
434           pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER)
435                                                 + pe_header->FileHeader.SizeOfOptionalHeader);
436           rootresdir = NULL;
437
438           /* search for the root resource directory */
439           for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
440           {
441             if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
442               continue;
443             if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
444               FIXME("File %s too short (section is at %d bytes, real size is %d)\n",
445                       debugstr_w(lpszExeFileName),
446                       pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
447                       fsizel
448               );
449               goto end;
450             }
451             /* FIXME: doesn't work when the resources are not in a separate section */
452             if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
453             {
454               rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
455               break;
456             }
457           }
458
459           if (!rootresdir)
460           {
461             WARN("haven't found section for resource directory.\n");
462             goto end;           /* failure */
463           }
464
465           /* search for the group icon directory */
466           if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
467           {
468             WARN("No Icongroupresourcedirectory!\n");
469             goto end;           /* failure */
470           }
471           iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
472
473           /* only number of icons requested */
474           if( !pIconId )
475           {
476             ret = iconDirCount;
477             goto end;           /* success */
478           }
479
480           if( nIconIndex < 0 )
481           {
482             /* search resource id */
483             int n = 0;
484             int iId = abs(nIconIndex);
485             const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
486
487             while(n<iconDirCount && xprdeTmp)
488             {
489               if(xprdeTmp->u1.s2.Id ==  iId)
490               {
491                   nIconIndex = n;
492                   break;
493               }
494               n++;
495               xprdeTmp++;
496             }
497             if (nIconIndex < 0)
498             {
499               WARN("resource id %d not found\n", iId);
500               goto end;         /* failure */
501             }
502           }
503           else
504           {
505             /* check nIconIndex to be in range */
506             if (nIconIndex >= iconDirCount)
507             {
508               WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
509               goto end;         /* failure */
510             }
511           }
512
513           /* assure we don't get too much */
514           if( nIcons > iconDirCount - nIconIndex )
515             nIcons = iconDirCount - nIconIndex;
516
517           /* starting from specified index */
518           xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
519
520           for (i=0; i < nIcons; i++,xresent++)
521           {
522             const IMAGE_RESOURCE_DIRECTORY *resdir;
523
524             /* go down this resource entry, name */
525             resdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)rootresdir + xresent->u2.s3.OffsetToDirectory);
526
527             /* default language (0) */
528             resdir = find_entry_default(resdir,rootresdir);
529             igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
530
531             /* lookup address in mapped image for virtual address */
532             igdata = NULL;
533
534             for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
535             {
536               if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
537                 continue;
538               if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
539                 continue;
540
541               if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
542                 FIXME("overflow in PE lookup (%s has len %d, have offset %d), short file?\n", debugstr_w(lpszExeFileName), fsizel,
543                            igdataent->OffsetToData - pe_sections[j].VirtualAddress + pe_sections[j].PointerToRawData + igdataent->Size);
544                 goto end; /* failure */
545               }
546               igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
547             }
548
549             if (!igdata)
550             {
551               FIXME("no matching real address for icongroup!\n");
552               goto end; /* failure */
553             }
554             pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
555             if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
556           }
557
558           if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
559           {
560             WARN("No Iconresourcedirectory!\n");
561             goto end;           /* failure */
562           }
563
564           for (i=0; i<nIcons; i++)
565           {
566             const IMAGE_RESOURCE_DIRECTORY *xresdir;
567             xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
568             if( !xresdir )
569             {
570               WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
571               RetPtr[i]=0;
572               continue;
573             }
574             xresdir = find_entry_default(xresdir, rootresdir);
575             idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
576             idata = NULL;
577
578             /* map virtual to address in image */
579             for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
580             {
581               if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
582                 continue;
583               if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
584                 continue;
585               idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
586             }
587             if (!idata)
588             {
589               WARN("no matching real address found for icondata!\n");
590               RetPtr[i]=0;
591               continue;
592             }
593             RetPtr[i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx1, cy1, flags);
594             if (cx2 && cy2)
595                 RetPtr[++i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx2, cy2, flags);
596           }
597           ret = i;      /* return number of retrieved icons */
598         }                       /* if(sig == IMAGE_NT_SIGNATURE) */
599
600 end:
601         UnmapViewOfFile(peimage);       /* success */
602         return ret;
603 }
604
605 /***********************************************************************
606  *           PrivateExtractIconsW                       [USER32.@]
607  *
608  * NOTES
609  *  If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
610  *  returned, with the LOWORD size icon first and the HIWORD size icon
611  *  second.
612  *  Also the Windows equivalent does extract icons in a strange way if
613  *  nIndex is negative. Our implementation treats a negative nIndex as
614  *  looking for that resource identifier for the first icon to retrieve.
615  *
616  * FIXME:
617  *  should also support 16 bit EXE + DLLs, cursor and animated cursor as
618  *  well as bitmap files.
619  */
620
621 UINT WINAPI PrivateExtractIconsW (
622         LPCWSTR lpwstrFile,
623         int nIndex,
624         int sizeX,
625         int sizeY,
626         HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
627         UINT* pIconId,  /* [out] pointer to array of nIcons icon identifiers or NULL */
628         UINT nIcons,    /* [in] number of icons to retrieve */
629         UINT flags )    /* [in] LR_* flags used by LoadImage */
630 {
631         TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
632               debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
633
634         if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
635         {
636           WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
637         }
638         return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
639 }
640
641 /***********************************************************************
642  *           PrivateExtractIconsA                       [USER32.@]
643  */
644
645 UINT WINAPI PrivateExtractIconsA (
646         LPCSTR lpstrFile,
647         int nIndex,
648         int sizeX,
649         int sizeY,
650         HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
651         UINT* piconid,  /* [out] pointer to array of nIcons icon identifiers or NULL */
652         UINT nIcons,    /* [in] number of icons to retrieve */
653         UINT flags )    /* [in] LR_* flags used by LoadImage */
654 {
655     UINT ret;
656     INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
657     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
658
659     MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
660     ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
661
662     HeapFree(GetProcessHeap(), 0, lpwstrFile);
663     return ret;
664 }
665
666 /***********************************************************************
667  *           PrivateExtractIconExW                      [USER32.@]
668  * NOTES
669  *  if nIndex == -1 it returns the number of icons in any case !!!
670  */
671 UINT WINAPI PrivateExtractIconExW (
672         LPCWSTR lpwstrFile,
673         int nIndex,
674         HICON * phIconLarge,
675         HICON * phIconSmall,
676         UINT nIcons )
677 {
678         DWORD cyicon, cysmicon, cxicon, cxsmicon;
679         UINT ret = 0;
680
681         TRACE("%s %d %p %p %d\n",
682         debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
683
684         if (nIndex == -1)
685           /* get the number of icons */
686           return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
687
688         if (nIcons == 1 && phIconSmall && phIconLarge)
689         {
690           HICON hIcon[2];
691           cxicon = GetSystemMetrics(SM_CXICON);
692           cyicon = GetSystemMetrics(SM_CYICON);
693           cxsmicon = GetSystemMetrics(SM_CXSMICON);
694           cysmicon = GetSystemMetrics(SM_CYSMICON);
695
696           ret = ICO_ExtractIconExW(lpwstrFile, hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
697                                    cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
698           *phIconLarge = hIcon[0];
699           *phIconSmall = hIcon[1];
700           return ret;
701         }
702
703         if (phIconSmall)
704         {
705           /* extract n small icons */
706           cxsmicon = GetSystemMetrics(SM_CXSMICON);
707           cysmicon = GetSystemMetrics(SM_CYSMICON);
708           ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
709                                    cysmicon, NULL, LR_DEFAULTCOLOR);
710         }
711        if (phIconLarge)
712         {
713           /* extract n large icons */
714           cxicon = GetSystemMetrics(SM_CXICON);
715           cyicon = GetSystemMetrics(SM_CYICON);
716          ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
717                                    cyicon, NULL, LR_DEFAULTCOLOR);
718         }
719         return ret;
720 }
721
722 /***********************************************************************
723  *           PrivateExtractIconExA                      [USER32.@]
724  */
725 UINT WINAPI PrivateExtractIconExA (
726         LPCSTR lpstrFile,
727         int nIndex,
728         HICON * phIconLarge,
729         HICON * phIconSmall,
730         UINT nIcons )
731 {
732         UINT ret;
733         INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
734         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
735
736         TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
737
738         MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
739         ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
740         HeapFree(GetProcessHeap(), 0, lpwstrFile);
741         return ret;
742 }