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