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