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