1c2c66affSColin Finck /*
2c2c66affSColin Finck * icon extracting
3c2c66affSColin Finck *
4c2c66affSColin Finck * taken and slightly changed from shell
5c2c66affSColin Finck * this should replace the icon extraction code in shell32 and shell16 once
6c2c66affSColin Finck * it needs a serious test for compliance with the native API
7c2c66affSColin Finck *
8c2c66affSColin Finck * Copyright 2000 Juergen Schmied
9c2c66affSColin Finck *
10c2c66affSColin Finck * This library is free software; you can redistribute it and/or
11c2c66affSColin Finck * modify it under the terms of the GNU Lesser General Public
12c2c66affSColin Finck * License as published by the Free Software Foundation; either
13c2c66affSColin Finck * version 2.1 of the License, or (at your option) any later version.
14c2c66affSColin Finck *
15c2c66affSColin Finck * This library is distributed in the hope that it will be useful,
16c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
17c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18c2c66affSColin Finck * Lesser General Public License for more details.
19c2c66affSColin Finck *
20c2c66affSColin Finck * You should have received a copy of the GNU Lesser General Public
21c2c66affSColin Finck * License along with this library; if not, write to the Free Software
22c2c66affSColin Finck * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23c2c66affSColin Finck */
24c2c66affSColin Finck
25c2c66affSColin Finck #include <user32.h>
26c2c66affSColin Finck
2740737bddSHermès Bélusca-Maïto #ifndef ARRAY_SIZE
2840737bddSHermès Bélusca-Maïto #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
2940737bddSHermès Bélusca-Maïto #endif
3040737bddSHermès Bélusca-Maïto
31c2c66affSColin Finck /* Start of Hack section */
32c2c66affSColin Finck
33c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(icon);
34c2c66affSColin Finck
35c2c66affSColin Finck #include <pshpack1.h>
36c2c66affSColin Finck
37c2c66affSColin Finck typedef struct
38c2c66affSColin Finck {
39c2c66affSColin Finck BYTE bWidth; /* Width, in pixels, of the image */
40c2c66affSColin Finck BYTE bHeight; /* Height, in pixels, of the image */
41c2c66affSColin Finck BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
42c2c66affSColin Finck BYTE bReserved; /* Reserved ( must be 0) */
43c2c66affSColin Finck WORD wPlanes; /* Color Planes */
44c2c66affSColin Finck WORD wBitCount; /* Bits per pixel */
45c2c66affSColin Finck DWORD dwBytesInRes; /* How many bytes in this resource? */
46c2c66affSColin Finck DWORD dwImageOffset; /* Where in the file is this image? */
47c2c66affSColin Finck } icoICONDIRENTRY, *LPicoICONDIRENTRY;
48c2c66affSColin Finck
49c2c66affSColin Finck typedef struct
50c2c66affSColin Finck {
51c2c66affSColin Finck WORD idReserved; /* Reserved (must be 0) */
52c2c66affSColin Finck WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
53c2c66affSColin Finck WORD idCount; /* How many images */
54c2c66affSColin Finck icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
55c2c66affSColin Finck } icoICONDIR, *LPicoICONDIR;
56c2c66affSColin Finck
57c2c66affSColin Finck typedef struct
58c2c66affSColin Finck {
59c2c66affSColin Finck WORD offset;
60c2c66affSColin Finck WORD length;
61c2c66affSColin Finck WORD flags;
62c2c66affSColin Finck WORD id;
63c2c66affSColin Finck WORD handle;
64c2c66affSColin Finck WORD usage;
65c2c66affSColin Finck } NE_NAMEINFO;
66c2c66affSColin Finck
67c2c66affSColin Finck typedef struct
68c2c66affSColin Finck {
69c2c66affSColin Finck WORD type_id;
70c2c66affSColin Finck WORD count;
71c2c66affSColin Finck DWORD resloader;
72c2c66affSColin Finck } NE_TYPEINFO;
73c2c66affSColin Finck
748a7b5a90SDoug Lyons #ifdef __REACTOS__
75a47590c9SDoug Lyons // From: James Houghtaling
76a47590c9SDoug Lyons // https://www.moon-soft.com/program/FORMAT/windows/ani.htm
77a47590c9SDoug Lyons typedef struct taganiheader
78a47590c9SDoug Lyons {
79a47590c9SDoug Lyons DWORD cbsizeof; // num bytes in aniheader (36 bytes)
80a47590c9SDoug Lyons DWORD cframes; // number of unique icons in this cursor
81a47590c9SDoug Lyons DWORD csteps; // number of blits before the animation cycles
82a47590c9SDoug Lyons DWORD cx; // reserved, must be zero.
83a47590c9SDoug Lyons DWORD cy; // reserved, must be zero.
84a47590c9SDoug Lyons DWORD cbitcount; // reserved, must be zero.
85a47590c9SDoug Lyons DWORD cplanes; // reserved, must be zero.
86a47590c9SDoug Lyons DWORD jifrate; // default jiffies (1/60th sec) if rate chunk not present.
87a47590c9SDoug Lyons DWORD flags; // animation flag
88a47590c9SDoug Lyons } aniheader;
898a7b5a90SDoug Lyons #endif
90a47590c9SDoug Lyons
91c2c66affSColin Finck #define NE_RSCTYPE_ICON 0x8003
92c2c66affSColin Finck #define NE_RSCTYPE_GROUP_ICON 0x800e
93c2c66affSColin Finck
94c2c66affSColin Finck #include <poppack.h>
95c2c66affSColin Finck
96c2c66affSColin Finck #if 0
97c2c66affSColin Finck static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
98c2c66affSColin Finck {
99c2c66affSColin Finck TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
100c2c66affSColin Finck TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
101c2c66affSColin Finck TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
102c2c66affSColin Finck entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
103c2c66affSColin Finck }
104c2c66affSColin Finck static void dumpIcoDir ( LPicoICONDIR entry )
105c2c66affSColin Finck {
106c2c66affSColin Finck TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
107c2c66affSColin Finck }
108c2c66affSColin Finck #endif
109c2c66affSColin Finck
110c2c66affSColin Finck #ifndef WINE
111c2c66affSColin Finck DWORD get_best_icon_file_offset(const LPBYTE dir,
112c2c66affSColin Finck DWORD dwFileSize,
113c2c66affSColin Finck int cxDesired,
114c2c66affSColin Finck int cyDesired,
115c2c66affSColin Finck BOOL bIcon,
116c2c66affSColin Finck DWORD fuLoad,
117c2c66affSColin Finck POINT *ptHotSpot);
118c2c66affSColin Finck #endif
119c2c66affSColin Finck
120c2c66affSColin Finck /**********************************************************************
121c2c66affSColin Finck * find_entry_by_id
122c2c66affSColin Finck *
123c2c66affSColin Finck * Find an entry by id in a resource directory
124c2c66affSColin Finck * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
125c2c66affSColin Finck */
find_entry_by_id(const IMAGE_RESOURCE_DIRECTORY * dir,WORD id,const void * root)126c2c66affSColin Finck static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
127c2c66affSColin Finck WORD id, const void *root )
128c2c66affSColin Finck {
129c2c66affSColin Finck const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
130c2c66affSColin Finck int min, max, pos;
131c2c66affSColin Finck
132c2c66affSColin Finck entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
133c2c66affSColin Finck min = dir->NumberOfNamedEntries;
134c2c66affSColin Finck max = min + dir->NumberOfIdEntries - 1;
135c2c66affSColin Finck while (min <= max)
136c2c66affSColin Finck {
137c2c66affSColin Finck pos = (min + max) / 2;
138c2c66affSColin Finck if (entry[pos].Id == id)
139c2c66affSColin Finck return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].OffsetToDirectory);
140c2c66affSColin Finck if (entry[pos].Id > id) max = pos - 1;
141c2c66affSColin Finck else min = pos + 1;
142c2c66affSColin Finck }
143c2c66affSColin Finck return NULL;
144c2c66affSColin Finck }
145c2c66affSColin Finck
146c2c66affSColin Finck /**********************************************************************
147c2c66affSColin Finck * find_entry_default
148c2c66affSColin Finck *
149c2c66affSColin Finck * Find a default entry in a resource directory
150c2c66affSColin Finck * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
151c2c66affSColin Finck */
find_entry_default(const IMAGE_RESOURCE_DIRECTORY * dir,const void * root)152c2c66affSColin Finck static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
153c2c66affSColin Finck const void *root )
154c2c66affSColin Finck {
155c2c66affSColin Finck const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
156c2c66affSColin Finck entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
157c2c66affSColin Finck return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->OffsetToDirectory);
158c2c66affSColin Finck }
159c2c66affSColin Finck
160c2c66affSColin Finck /*************************************************************************
161c2c66affSColin Finck * USER32_GetResourceTable
162c2c66affSColin Finck */
USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE * retptr)163c2c66affSColin Finck static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
164c2c66affSColin Finck {
165c2c66affSColin Finck IMAGE_DOS_HEADER * mz_header;
166c2c66affSColin Finck
167c2c66affSColin Finck TRACE("%p %p\n", peimage, retptr);
168c2c66affSColin Finck
169c2c66affSColin Finck *retptr = NULL;
170c2c66affSColin Finck
171c2c66affSColin Finck mz_header = (IMAGE_DOS_HEADER*) peimage;
172c2c66affSColin Finck
173c2c66affSColin Finck if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
174c2c66affSColin Finck {
175c2c66affSColin Finck if (mz_header->e_cblp == 1 || mz_header->e_cblp == 2) /* .ICO or .CUR file ? */
176c2c66affSColin Finck {
177c2c66affSColin Finck *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
178c2c66affSColin Finck return mz_header->e_cblp;
179c2c66affSColin Finck }
180c2c66affSColin Finck else
181c2c66affSColin Finck return 0; /* failed */
182c2c66affSColin Finck }
183c2c66affSColin Finck if (mz_header->e_lfanew >= pesize) {
184c2c66affSColin Finck return 0; /* failed, happens with PKZIP DOS Exes for instance. */
185c2c66affSColin Finck }
186c2c66affSColin Finck if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
187c2c66affSColin Finck return IMAGE_NT_SIGNATURE;
188c2c66affSColin Finck
189c2c66affSColin Finck if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
190c2c66affSColin Finck {
191c2c66affSColin Finck IMAGE_OS2_HEADER * ne_header;
192c2c66affSColin Finck
193c2c66affSColin Finck ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
194c2c66affSColin Finck
195c2c66affSColin Finck if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
196c2c66affSColin Finck return 0;
197c2c66affSColin Finck
198c2c66affSColin Finck if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
199c2c66affSColin Finck *retptr = (LPBYTE)-1;
200c2c66affSColin Finck else
201c2c66affSColin Finck *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
202c2c66affSColin Finck
203c2c66affSColin Finck return IMAGE_OS2_SIGNATURE;
204c2c66affSColin Finck }
205c2c66affSColin Finck return 0; /* failed */
206c2c66affSColin Finck }
207c2c66affSColin Finck /*************************************************************************
208c2c66affSColin Finck * USER32_LoadResource
209c2c66affSColin Finck */
USER32_LoadResource(LPBYTE peimage,NE_NAMEINFO * pNInfo,WORD sizeShift,ULONG * uSize)210c2c66affSColin Finck static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
211c2c66affSColin Finck {
212c2c66affSColin Finck TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
213c2c66affSColin Finck
214c2c66affSColin Finck *uSize = (DWORD)pNInfo->length << sizeShift;
215c2c66affSColin Finck return peimage + ((DWORD)pNInfo->offset << sizeShift);
216c2c66affSColin Finck }
217c2c66affSColin Finck
218c2c66affSColin Finck /*************************************************************************
219c2c66affSColin Finck * ICO_LoadIcon
220c2c66affSColin Finck */
ICO_LoadIcon(LPBYTE peimage,LPicoICONDIRENTRY lpiIDE,ULONG * uSize)221c2c66affSColin Finck static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
222c2c66affSColin Finck {
223c2c66affSColin Finck TRACE("%p %p\n", peimage, lpiIDE);
224c2c66affSColin Finck
225c2c66affSColin Finck *uSize = lpiIDE->dwBytesInRes;
226c2c66affSColin Finck return peimage + lpiIDE->dwImageOffset;
227c2c66affSColin Finck }
228c2c66affSColin Finck
229c2c66affSColin Finck /*************************************************************************
230c2c66affSColin Finck * ICO_GetIconDirectory
231c2c66affSColin Finck *
232c2c66affSColin Finck * Reads .ico file and build phony ICONDIR struct
233c2c66affSColin Finck */
ICO_GetIconDirectory(LPBYTE peimage,LPicoICONDIR * lplpiID,ULONG * uSize)234c2c66affSColin Finck static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
235c2c66affSColin Finck {
236c2c66affSColin Finck CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
237c2c66affSColin Finck CURSORICONDIR * lpID; /* icon resource in resource format */
238c2c66affSColin Finck int i;
239c2c66affSColin Finck
240c2c66affSColin Finck TRACE("%p %p\n", peimage, lplpiID);
241c2c66affSColin Finck
242c2c66affSColin Finck lpcid = (CURSORICONDIR*)peimage;
243c2c66affSColin Finck
244c2c66affSColin Finck if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
245c2c66affSColin Finck return 0;
246c2c66affSColin Finck
247c2c66affSColin Finck /* allocate the phony ICONDIR structure */
248c2c66affSColin Finck *uSize = FIELD_OFFSET(CURSORICONDIR, idEntries[lpcid->idCount]);
249c2c66affSColin Finck if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
250c2c66affSColin Finck {
251c2c66affSColin Finck /* copy the header */
252c2c66affSColin Finck lpID->idReserved = lpcid->idReserved;
253c2c66affSColin Finck lpID->idType = lpcid->idType;
254c2c66affSColin Finck lpID->idCount = lpcid->idCount;
255c2c66affSColin Finck
256c2c66affSColin Finck /* copy the entries */
257c2c66affSColin Finck for( i=0; i < lpcid->idCount; i++ )
258c2c66affSColin Finck {
259c2c66affSColin Finck memcpy(&lpID->idEntries[i], &lpcid->idEntries[i], sizeof(CURSORICONDIRENTRY) - 2);
260c2c66affSColin Finck lpID->idEntries[i].wResId = i;
261c2c66affSColin Finck }
262c2c66affSColin Finck
263c2c66affSColin Finck *lplpiID = (LPicoICONDIR)peimage;
264c2c66affSColin Finck return (BYTE *)lpID;
265c2c66affSColin Finck }
266c2c66affSColin Finck return 0;
267c2c66affSColin Finck }
268c2c66affSColin Finck
269c2c66affSColin Finck /*************************************************************************
270c2c66affSColin Finck * ICO_ExtractIconExW [internal]
271c2c66affSColin Finck *
272c2c66affSColin Finck * NOTES
273c2c66affSColin Finck * nIcons = 0: returns number of Icons in file
274c2c66affSColin Finck *
275c2c66affSColin Finck * returns
276c2c66affSColin Finck * invalid file: -1
277c2c66affSColin Finck * failure:0;
278c2c66affSColin Finck * success: number of icons in file (nIcons = 0) or nr of icons retrieved
279c2c66affSColin Finck */
ICO_ExtractIconExW(LPCWSTR lpszExeFileName,HICON * RetPtr,INT nIconIndex,UINT nIcons,UINT cxDesired,UINT cyDesired,UINT * pIconId,UINT flags,BOOL fIconEx)280c2c66affSColin Finck static UINT ICO_ExtractIconExW(
281c2c66affSColin Finck LPCWSTR lpszExeFileName,
282c2c66affSColin Finck HICON * RetPtr,
283c2c66affSColin Finck INT nIconIndex,
284c2c66affSColin Finck UINT nIcons,
285c2c66affSColin Finck UINT cxDesired,
286c2c66affSColin Finck UINT cyDesired,
287c2c66affSColin Finck UINT *pIconId,
2888a7b5a90SDoug Lyons #ifdef __REACTOS__
2898a7b5a90SDoug Lyons UINT flags,
2908a7b5a90SDoug Lyons /* This function is called from two different code paths.
2918a7b5a90SDoug Lyons * One is from Shell32 using the ExtractIconEx function.
2928a7b5a90SDoug Lyons * The other is from User32 using PrivateExtractIcons.
2938a7b5a90SDoug Lyons * Based on W2K3SP2 testing, the count of icons returned
2948a7b5a90SDoug Lyons * is zero (0) for PNG ones using ExtractIconEx and
2958a7b5a90SDoug Lyons * one (1) for PNG icons using PrivateExtractIcons.
2968a7b5a90SDoug Lyons * We can handle the difference using the fIconEx flag.*/
2978a7b5a90SDoug Lyons BOOL fIconEx)
2988a7b5a90SDoug Lyons #else
299c2c66affSColin Finck UINT flags)
3008a7b5a90SDoug Lyons #endif
301c2c66affSColin Finck {
302c2c66affSColin Finck UINT ret = 0;
303c2c66affSColin Finck UINT cx1, cx2, cy1, cy2;
304c2c66affSColin Finck LPBYTE pData;
305c2c66affSColin Finck DWORD sig;
306c2c66affSColin Finck HANDLE hFile;
307c2c66affSColin Finck UINT16 iconDirCount = 0,iconCount = 0;
308c2c66affSColin Finck LPBYTE peimage;
309c2c66affSColin Finck HANDLE fmapping;
310c2c66affSColin Finck DWORD fsizeh,fsizel;
31140737bddSHermès Bélusca-Maïto #ifdef __REACTOS__
31240737bddSHermès Bélusca-Maïto WCHAR szExpandedExePath[MAX_PATH];
31340737bddSHermès Bélusca-Maïto #endif
314c2c66affSColin Finck WCHAR szExePath[MAX_PATH];
315c2c66affSColin Finck DWORD dwSearchReturn;
316c2c66affSColin Finck
3178a7b5a90SDoug Lyons #ifdef __REACTOS__
3188a7b5a90SDoug Lyons TRACE("%s, %d, %d, %p, 0x%08x, %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags, fIconEx);
3198a7b5a90SDoug Lyons #else
320c2c66affSColin Finck TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
3218a7b5a90SDoug Lyons #endif
322c2c66affSColin Finck
32340737bddSHermès Bélusca-Maïto #ifdef __REACTOS__
32440737bddSHermès Bélusca-Maïto if (RetPtr)
32540737bddSHermès Bélusca-Maïto *RetPtr = NULL;
32640737bddSHermès Bélusca-Maïto
32740737bddSHermès Bélusca-Maïto if (ExpandEnvironmentStringsW(lpszExeFileName, szExpandedExePath, ARRAY_SIZE(szExpandedExePath)))
32840737bddSHermès Bélusca-Maïto lpszExeFileName = szExpandedExePath;
32940737bddSHermès Bélusca-Maïto #endif
33040737bddSHermès Bélusca-Maïto
33140737bddSHermès Bélusca-Maïto dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, ARRAY_SIZE(szExePath), szExePath, NULL);
33240737bddSHermès Bélusca-Maïto if ((dwSearchReturn == 0) || (dwSearchReturn > ARRAY_SIZE(szExePath)))
333c2c66affSColin Finck {
3348a7b5a90SDoug Lyons #ifdef __REACTOS__
3358a7b5a90SDoug Lyons WARN("File %s not found or path too long and fIconEx is '%d'\n",
3368a7b5a90SDoug Lyons debugstr_w(lpszExeFileName), fIconEx);
3378a7b5a90SDoug Lyons if (fIconEx && !RetPtr && !pIconId)
3388a7b5a90SDoug Lyons return 0;
3398a7b5a90SDoug Lyons else
3408a7b5a90SDoug Lyons return -1;
3418a7b5a90SDoug Lyons #else
342c2c66affSColin Finck WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
343c2c66affSColin Finck return -1;
3448a7b5a90SDoug Lyons #endif
345c2c66affSColin Finck }
346c2c66affSColin Finck
347c2c66affSColin Finck hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
348c2c66affSColin Finck if (hFile == INVALID_HANDLE_VALUE) return ret;
349c2c66affSColin Finck fsizel = GetFileSize(hFile,&fsizeh);
350c2c66affSColin Finck
351c2c66affSColin Finck /* Map the file */
352c2c66affSColin Finck fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
353c2c66affSColin Finck CloseHandle(hFile);
354c2c66affSColin Finck if (!fmapping)
355c2c66affSColin Finck {
356c2c66affSColin Finck WARN("CreateFileMapping error %ld\n", GetLastError() );
357c2c66affSColin Finck return 0xFFFFFFFF;
358c2c66affSColin Finck }
359c2c66affSColin Finck
360c2c66affSColin Finck if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
361c2c66affSColin Finck {
362c2c66affSColin Finck WARN("MapViewOfFile error %ld\n", GetLastError() );
363c2c66affSColin Finck CloseHandle(fmapping);
364c2c66affSColin Finck return 0xFFFFFFFF;
365c2c66affSColin Finck }
366c2c66affSColin Finck CloseHandle(fmapping);
367c2c66affSColin Finck
368a47590c9SDoug Lyons #ifdef __REACTOS__
369a47590c9SDoug Lyons /* Check if we have a min size of 2 headers RIFF & 'icon'
370a47590c9SDoug Lyons * at 8 chars each plus an anih header of 36 byptes.
371a47590c9SDoug Lyons * Also, is this resource an animjated icon/cursor (RIFF) */
372a47590c9SDoug Lyons if ((fsizel >= 52) && !memcmp(peimage, "RIFF", 4))
373a47590c9SDoug Lyons {
374a47590c9SDoug Lyons UINT anihOffset;
375a47590c9SDoug Lyons UINT anihMax;
376a47590c9SDoug Lyons /* Get size of the animation data */
377a47590c9SDoug Lyons ULONG uSize = MAKEWORD(peimage[4], peimage[5]);
378a47590c9SDoug Lyons
379a47590c9SDoug Lyons /* Check if uSize is reasonable with respect to fsizel */
380a47590c9SDoug Lyons if ((uSize < strlen("anih")) || (uSize > fsizel))
381a47590c9SDoug Lyons goto end;
382a47590c9SDoug Lyons
383a47590c9SDoug Lyons /* Look though the reported size less search string length */
384a47590c9SDoug Lyons anihMax = uSize - strlen("anih");
385a47590c9SDoug Lyons /* Search for 'anih' indicating animation header */
386a47590c9SDoug Lyons for (anihOffset = 0; anihOffset < anihMax; anihOffset++)
387a47590c9SDoug Lyons {
388a47590c9SDoug Lyons if (memcmp(&peimage[anihOffset], "anih", 4) == 0)
389a47590c9SDoug Lyons break;
390a47590c9SDoug Lyons }
391a47590c9SDoug Lyons
392a47590c9SDoug Lyons if (anihOffset + sizeof(aniheader) > fsizel)
393a47590c9SDoug Lyons goto end;
394a47590c9SDoug Lyons
395a47590c9SDoug Lyons /* Get count of images for return value */
396a47590c9SDoug Lyons ret = MAKEWORD(peimage[anihOffset + 12], peimage[anihOffset + 13]);
397a47590c9SDoug Lyons
398a47590c9SDoug Lyons TRACE("RIFF File with '%u' images at Offset '%u'.\n", ret, anihOffset);
399a47590c9SDoug Lyons
400a47590c9SDoug Lyons cx1 = LOWORD(cxDesired);
401a47590c9SDoug Lyons cy1 = LOWORD(cyDesired);
402a47590c9SDoug Lyons
403a47590c9SDoug Lyons if (RetPtr)
404a47590c9SDoug Lyons {
405a47590c9SDoug Lyons RetPtr[0] = CreateIconFromResourceEx(peimage, uSize, TRUE, 0x00030000, cx1, cy1, flags);
406a47590c9SDoug Lyons }
407a47590c9SDoug Lyons goto end;
408a47590c9SDoug Lyons }
409a47590c9SDoug Lyons #endif
410a47590c9SDoug Lyons
411c2c66affSColin Finck cx1 = LOWORD(cxDesired);
412c2c66affSColin Finck cx2 = HIWORD(cxDesired);
413c2c66affSColin Finck cy1 = LOWORD(cyDesired);
414c2c66affSColin Finck cy2 = HIWORD(cyDesired);
415c2c66affSColin Finck
416c2c66affSColin Finck if (pIconId) /* Invalidate first icon identifier */
417c2c66affSColin Finck *pIconId = 0xFFFFFFFF;
418c2c66affSColin Finck
419c2c66affSColin Finck if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
420c2c66affSColin Finck pIconId = (UINT*)RetPtr;
421c2c66affSColin Finck
422c2c66affSColin Finck sig = USER32_GetResourceTable(peimage, fsizel, &pData);
423c2c66affSColin Finck
424c2c66affSColin Finck /* NE exe/dll */
425c2c66affSColin Finck if (sig==IMAGE_OS2_SIGNATURE)
426c2c66affSColin Finck {
427c2c66affSColin Finck BYTE *pCIDir = 0;
428c2c66affSColin Finck NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
429c2c66affSColin Finck NE_NAMEINFO *pIconStorage = NULL;
430c2c66affSColin Finck NE_NAMEINFO *pIconDir = NULL;
431c2c66affSColin Finck LPicoICONDIR lpiID = NULL;
432c2c66affSColin Finck ULONG uSize = 0;
433c2c66affSColin Finck
434c2c66affSColin Finck TRACE("-- OS2/icon Signature (0x%08x)\n", sig);
435c2c66affSColin Finck
436c2c66affSColin Finck if (pData == (BYTE*)-1)
437c2c66affSColin Finck {
438c2c66affSColin Finck pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
439c2c66affSColin Finck if (pCIDir)
440c2c66affSColin Finck {
441c2c66affSColin Finck iconDirCount = 1; iconCount = lpiID->idCount;
442c2c66affSColin Finck TRACE("-- icon found %p 0x%08x 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
443c2c66affSColin Finck }
444c2c66affSColin Finck }
445c2c66affSColin Finck else while (pTInfo->type_id && !(pIconStorage && pIconDir))
446c2c66affSColin Finck {
447c2c66affSColin Finck if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON) /* find icon directory and icon repository */
448c2c66affSColin Finck {
449c2c66affSColin Finck iconDirCount = pTInfo->count;
450c2c66affSColin Finck pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
451c2c66affSColin Finck TRACE("\tfound directory - %i icon families\n", iconDirCount);
452c2c66affSColin Finck }
453c2c66affSColin Finck if (pTInfo->type_id == NE_RSCTYPE_ICON)
454c2c66affSColin Finck {
455c2c66affSColin Finck iconCount = pTInfo->count;
456c2c66affSColin Finck pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
457c2c66affSColin Finck TRACE("\ttotal icons - %i\n", iconCount);
458c2c66affSColin Finck }
459c2c66affSColin Finck pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
460c2c66affSColin Finck }
461c2c66affSColin Finck
462c2c66affSColin Finck if ((pIconStorage && pIconDir) || lpiID) /* load resources and create icons */
463c2c66affSColin Finck {
464c2c66affSColin Finck if (nIcons == 0)
465c2c66affSColin Finck {
466c2c66affSColin Finck ret = iconDirCount;
467c2c66affSColin Finck if (lpiID) /* *.ico file, deallocate heap pointer*/
468c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, pCIDir);
469c2c66affSColin Finck }
470c2c66affSColin Finck else if (nIconIndex < iconDirCount)
471c2c66affSColin Finck {
472c2c66affSColin Finck UINT16 i, icon;
473c2c66affSColin Finck if (nIcons > iconDirCount - nIconIndex)
474c2c66affSColin Finck nIcons = iconDirCount - nIconIndex;
475c2c66affSColin Finck
476c2c66affSColin Finck for (i = 0; i < nIcons; i++)
477c2c66affSColin Finck {
478c2c66affSColin Finck /* .ICO files have only one icon directory */
479c2c66affSColin Finck if (lpiID == NULL) /* not *.ico */
480c2c66affSColin Finck pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
481c2c66affSColin Finck pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
482c2c66affSColin Finck if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx2, cy2, flags);
483c2c66affSColin Finck }
484c2c66affSColin Finck if (lpiID) /* *.ico file, deallocate heap pointer*/
485c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, pCIDir);
486c2c66affSColin Finck
487c2c66affSColin Finck for (icon = 0; icon < nIcons; icon++)
488c2c66affSColin Finck {
489c2c66affSColin Finck pCIDir = NULL;
490c2c66affSColin Finck if (lpiID)
491c2c66affSColin Finck pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
492c2c66affSColin Finck else
493c2c66affSColin Finck for (i = 0; i < iconCount; i++)
494c2c66affSColin Finck if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
495c2c66affSColin Finck pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
496c2c66affSColin Finck
497c2c66affSColin Finck if (pCIDir)
498c2c66affSColin Finck {
499c2c66affSColin Finck RetPtr[icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
500c2c66affSColin Finck cx1, cy1, flags);
501c2c66affSColin Finck if (cx2 && cy2)
502c2c66affSColin Finck RetPtr[++icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
503c2c66affSColin Finck cx2, cy2, flags);
504c2c66affSColin Finck }
505c2c66affSColin Finck else
506c2c66affSColin Finck RetPtr[icon] = 0;
507c2c66affSColin Finck }
508c2c66affSColin Finck ret = icon; /* return number of retrieved icons */
509c2c66affSColin Finck }
510c2c66affSColin Finck }
511c2c66affSColin Finck }
512c2c66affSColin Finck else
513c2c66affSColin Finck if (sig == 1 || sig == 2) /* .ICO or .CUR file */
514c2c66affSColin Finck {
515c2c66affSColin Finck TRACE("-- icon Signature (0x%08x)\n", sig);
516c2c66affSColin Finck
517c2c66affSColin Finck if (pData == (BYTE*)-1)
518c2c66affSColin Finck {
519c2c66affSColin Finck INT cx[2] = {cx1, cx2}, cy[2] = {cy1, cy2};
520c2c66affSColin Finck INT index;
521c2c66affSColin Finck
522c2c66affSColin Finck for(index = 0; index < (cx2 || cy2 ? 2 : 1); index++)
523c2c66affSColin Finck {
524c2c66affSColin Finck DWORD dataOffset;
525c2c66affSColin Finck LPBYTE imageData;
526c2c66affSColin Finck POINT hotSpot;
527a47590c9SDoug Lyons #ifndef __REACTOS__
528c2c66affSColin Finck LPICONIMAGE entry;
529a47590c9SDoug Lyons #endif
530c2c66affSColin Finck
531c2c66affSColin Finck dataOffset = get_best_icon_file_offset(peimage, fsizel, cx[index], cy[index], sig == 1, flags, sig == 1 ? NULL : &hotSpot);
532c2c66affSColin Finck
533c2c66affSColin Finck if (dataOffset)
534c2c66affSColin Finck {
535c2c66affSColin Finck HICON icon;
536c2c66affSColin Finck WORD *cursorData = NULL;
537a47590c9SDoug Lyons #ifdef __REACTOS__
5380360abb7SKatayama Hirofumi MZ BITMAPINFOHEADER bi;
5390360abb7SKatayama Hirofumi MZ DWORD cbColorTable = 0, cbTotal;
540a47590c9SDoug Lyons #endif
541c2c66affSColin Finck
542c2c66affSColin Finck imageData = peimage + dataOffset;
543a47590c9SDoug Lyons #ifdef __REACTOS__
5440360abb7SKatayama Hirofumi MZ /* Calculate the size of color table */
5455972c0c1SKatayama Hirofumi MZ ZeroMemory(&bi, sizeof(bi));
5460360abb7SKatayama Hirofumi MZ CopyMemory(&bi, imageData, sizeof(BITMAPCOREHEADER));
5470360abb7SKatayama Hirofumi MZ if (bi.biBitCount <= 8)
5480360abb7SKatayama Hirofumi MZ {
5490360abb7SKatayama Hirofumi MZ if (bi.biSize >= sizeof(BITMAPINFOHEADER))
5500360abb7SKatayama Hirofumi MZ {
5510360abb7SKatayama Hirofumi MZ CopyMemory(&bi, imageData, sizeof(BITMAPINFOHEADER));
5520360abb7SKatayama Hirofumi MZ if (bi.biClrUsed)
5530360abb7SKatayama Hirofumi MZ cbColorTable = bi.biClrUsed * sizeof(RGBQUAD);
5540360abb7SKatayama Hirofumi MZ else
5550360abb7SKatayama Hirofumi MZ cbColorTable = (1 << bi.biBitCount) * sizeof(RGBQUAD);
5560360abb7SKatayama Hirofumi MZ }
5570360abb7SKatayama Hirofumi MZ else if (bi.biSize == sizeof(BITMAPCOREHEADER))
5580360abb7SKatayama Hirofumi MZ {
5590360abb7SKatayama Hirofumi MZ cbColorTable = (1 << bi.biBitCount) * sizeof(RGBTRIPLE);
5600360abb7SKatayama Hirofumi MZ }
5610360abb7SKatayama Hirofumi MZ }
562c2c66affSColin Finck
563a47590c9SDoug Lyons /* biSizeImage is the size of the raw bitmap data.
564a47590c9SDoug Lyons * https://en.wikipedia.org/wiki/BMP_file_format */
5651cad26adSKatayama Hirofumi MZ if (bi.biSizeImage == 0)
566a47590c9SDoug Lyons {
5670360abb7SKatayama Hirofumi MZ /* Calculate image size */
5680360abb7SKatayama Hirofumi MZ #define WIDTHBYTES(width, bits) (((width) * (bits) + 31) / 32 * 4)
5690360abb7SKatayama Hirofumi MZ bi.biSizeImage = WIDTHBYTES(bi.biWidth, bi.biBitCount) * (bi.biHeight / 2);
5700360abb7SKatayama Hirofumi MZ bi.biSizeImage += WIDTHBYTES(bi.biWidth, 1) * (bi.biHeight / 2);
5710360abb7SKatayama Hirofumi MZ #undef WIDTHBYTES
572a47590c9SDoug Lyons }
5731cad26adSKatayama Hirofumi MZ
5740360abb7SKatayama Hirofumi MZ /* Calculate total size */
5750360abb7SKatayama Hirofumi MZ cbTotal = bi.biSize + cbColorTable + bi.biSizeImage;
5761cad26adSKatayama Hirofumi MZ #else
5771cad26adSKatayama Hirofumi MZ entry = (LPICONIMAGE)(imageData);
578a47590c9SDoug Lyons #endif
5791cad26adSKatayama Hirofumi MZ
5801cad26adSKatayama Hirofumi MZ if(sig == 2)
5811cad26adSKatayama Hirofumi MZ {
582c2c66affSColin Finck /* we need to prepend the bitmap data with hot spots for CreateIconFromResourceEx */
583a47590c9SDoug Lyons #ifdef __REACTOS__
5840360abb7SKatayama Hirofumi MZ cursorData = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(WORD) + cbTotal);
585a47590c9SDoug Lyons #else
586c2c66affSColin Finck cursorData = HeapAlloc(GetProcessHeap(), 0, entry->icHeader.biSizeImage + 2 * sizeof(WORD));
587a47590c9SDoug Lyons #endif
588c2c66affSColin Finck
589c2c66affSColin Finck if(!cursorData)
590c2c66affSColin Finck continue;
591c2c66affSColin Finck
592c2c66affSColin Finck cursorData[0] = hotSpot.x;
593c2c66affSColin Finck cursorData[1] = hotSpot.y;
594c2c66affSColin Finck
595a47590c9SDoug Lyons #ifdef __REACTOS__
5960360abb7SKatayama Hirofumi MZ CopyMemory(cursorData + 2, imageData, cbTotal);
597a47590c9SDoug Lyons #else
598c2c66affSColin Finck memcpy(cursorData + 2, imageData, entry->icHeader.biSizeImage);
599a47590c9SDoug Lyons #endif
600c2c66affSColin Finck
601c2c66affSColin Finck imageData = (LPBYTE)cursorData;
602c2c66affSColin Finck }
603c2c66affSColin Finck
604a47590c9SDoug Lyons #ifdef __REACTOS__
6050360abb7SKatayama Hirofumi MZ icon = CreateIconFromResourceEx(imageData, cbTotal, sig == 1, 0x00030000, cx[index], cy[index], flags);
6068a7b5a90SDoug Lyons if (fIconEx && sig == 1)
6078a7b5a90SDoug Lyons iconCount = 1;
608a47590c9SDoug Lyons #else
609c2c66affSColin Finck icon = CreateIconFromResourceEx(imageData, entry->icHeader.biSizeImage, sig == 1, 0x00030000, cx[index], cy[index], flags);
610a47590c9SDoug Lyons #endif
611c2c66affSColin Finck
6122af9e7dfSKatayama Hirofumi MZ HeapFree(GetProcessHeap(), 0, cursorData);
6132af9e7dfSKatayama Hirofumi MZ
614c2c66affSColin Finck if (icon)
615c2c66affSColin Finck {
6162af9e7dfSKatayama Hirofumi MZ if (RetPtr)
617c2c66affSColin Finck RetPtr[index] = icon;
6182af9e7dfSKatayama Hirofumi MZ else
6192af9e7dfSKatayama Hirofumi MZ DestroyIcon(icon);
6202af9e7dfSKatayama Hirofumi MZ
621c2c66affSColin Finck iconCount = 1;
6222af9e7dfSKatayama Hirofumi MZ break;
623c2c66affSColin Finck }
624c2c66affSColin Finck }
6252af9e7dfSKatayama Hirofumi MZ }
626c2c66affSColin Finck }
627c2c66affSColin Finck ret = iconCount; /* return number of retrieved icons */
628c2c66affSColin Finck }
629c2c66affSColin Finck /* end ico file */
630c2c66affSColin Finck
631c2c66affSColin Finck /* exe/dll */
632c2c66affSColin Finck else if( sig == IMAGE_NT_SIGNATURE )
633c2c66affSColin Finck {
634c2c66affSColin Finck BYTE *idata, *igdata;
635c2c66affSColin Finck const IMAGE_RESOURCE_DIRECTORY *rootresdir, *iconresdir, *icongroupresdir;
636c2c66affSColin Finck const IMAGE_RESOURCE_DATA_ENTRY *idataent, *igdataent;
637c2c66affSColin Finck const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
638c2c66affSColin Finck ULONG size;
639c2c66affSColin Finck UINT i;
640c2c66affSColin Finck
641c2c66affSColin Finck rootresdir = RtlImageDirectoryEntryToData((HMODULE)peimage, FALSE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size);
642c2c66affSColin Finck if (!rootresdir)
643c2c66affSColin Finck {
644c2c66affSColin Finck WARN("haven't found section for resource directory.\n");
645c2c66affSColin Finck goto end;
646c2c66affSColin Finck }
647c2c66affSColin Finck
64860851914SDoug Lyons #ifdef __REACTOS__
64960851914SDoug Lyons /* Check for boundary limit (and overflow) */
65060851914SDoug Lyons if (((ULONG_PTR)(rootresdir + 1) < (ULONG_PTR)rootresdir) ||
65160851914SDoug Lyons ((ULONG_PTR)(rootresdir + 1) > (ULONG_PTR)peimage + fsizel))
65260851914SDoug Lyons {
65360851914SDoug Lyons goto end;
65460851914SDoug Lyons }
65560851914SDoug Lyons #endif
65660851914SDoug Lyons
657c2c66affSColin Finck /* search for the group icon directory */
658c2c66affSColin Finck if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
659c2c66affSColin Finck {
660c2c66affSColin Finck WARN("No Icongroupresourcedirectory!\n");
661c2c66affSColin Finck goto end; /* failure */
662c2c66affSColin Finck }
663c2c66affSColin Finck iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
664c2c66affSColin Finck
665c2c66affSColin Finck /* only number of icons requested */
666c2c66affSColin Finck if( !pIconId )
667c2c66affSColin Finck {
668c2c66affSColin Finck ret = iconDirCount;
669c2c66affSColin Finck goto end; /* success */
670c2c66affSColin Finck }
671c2c66affSColin Finck
672c2c66affSColin Finck if( nIconIndex < 0 )
673c2c66affSColin Finck {
674c2c66affSColin Finck /* search resource id */
675c2c66affSColin Finck int n = 0;
676c2c66affSColin Finck int iId = abs(nIconIndex);
677c2c66affSColin Finck const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
678c2c66affSColin Finck
679c2c66affSColin Finck while(n<iconDirCount && xprdeTmp)
680c2c66affSColin Finck {
681c2c66affSColin Finck if(xprdeTmp->Id == iId)
682c2c66affSColin Finck {
683c2c66affSColin Finck nIconIndex = n;
684c2c66affSColin Finck break;
685c2c66affSColin Finck }
686c2c66affSColin Finck n++;
687c2c66affSColin Finck xprdeTmp++;
688c2c66affSColin Finck }
689c2c66affSColin Finck if (nIconIndex < 0)
690c2c66affSColin Finck {
691c2c66affSColin Finck WARN("resource id %d not found\n", iId);
692c2c66affSColin Finck goto end; /* failure */
693c2c66affSColin Finck }
694c2c66affSColin Finck }
695c2c66affSColin Finck else
696c2c66affSColin Finck {
697c2c66affSColin Finck /* check nIconIndex to be in range */
698c2c66affSColin Finck if (nIconIndex >= iconDirCount)
699c2c66affSColin Finck {
700c2c66affSColin Finck WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
701c2c66affSColin Finck goto end; /* failure */
702c2c66affSColin Finck }
703c2c66affSColin Finck }
704c2c66affSColin Finck
705c2c66affSColin Finck /* assure we don't get too much */
706c2c66affSColin Finck if( nIcons > iconDirCount - nIconIndex )
707c2c66affSColin Finck nIcons = iconDirCount - nIconIndex;
708c2c66affSColin Finck
709c2c66affSColin Finck /* starting from specified index */
710c2c66affSColin Finck xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
711c2c66affSColin Finck
712c2c66affSColin Finck for (i=0; i < nIcons; i++,xresent++)
713c2c66affSColin Finck {
714c2c66affSColin Finck const IMAGE_RESOURCE_DIRECTORY *resdir;
715c2c66affSColin Finck
716c2c66affSColin Finck /* go down this resource entry, name */
717c2c66affSColin Finck resdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)rootresdir + xresent->OffsetToDirectory);
718c2c66affSColin Finck
719c2c66affSColin Finck /* default language (0) */
720c2c66affSColin Finck resdir = find_entry_default(resdir,rootresdir);
721c2c66affSColin Finck igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
722c2c66affSColin Finck
723c2c66affSColin Finck /* lookup address in mapped image for virtual address */
724c2c66affSColin Finck igdata = RtlImageRvaToVa(RtlImageNtHeader((HMODULE)peimage), (HMODULE)peimage, igdataent->OffsetToData, NULL);
725c2c66affSColin Finck if (!igdata)
726c2c66affSColin Finck {
727c2c66affSColin Finck FIXME("no matching real address for icongroup!\n");
728c2c66affSColin Finck goto end; /* failure */
729c2c66affSColin Finck }
730c2c66affSColin Finck pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
731c2c66affSColin Finck if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
732c2c66affSColin Finck }
733c2c66affSColin Finck
734c2c66affSColin Finck if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
735c2c66affSColin Finck {
736c2c66affSColin Finck WARN("No Iconresourcedirectory!\n");
737c2c66affSColin Finck goto end; /* failure */
738c2c66affSColin Finck }
739c2c66affSColin Finck
740c2c66affSColin Finck for (i=0; i<nIcons; i++)
741c2c66affSColin Finck {
742c2c66affSColin Finck const IMAGE_RESOURCE_DIRECTORY *xresdir;
743c2c66affSColin Finck xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
744c2c66affSColin Finck if( !xresdir )
745c2c66affSColin Finck {
746*75c67f9bSDoug Lyons #ifdef __REACTOS__
747*75c67f9bSDoug Lyons /* XP/2K3 can decode icons this way. Vista/7 cannot. This handles
748*75c67f9bSDoug Lyons * damaged Resources in 'Icon Group' by falling back to 'Icon' resources.
749*75c67f9bSDoug Lyons * Older Watcom C/C++ compilers can generate such a case */
750*75c67f9bSDoug Lyons const IMAGE_RESOURCE_DIRECTORY *resdir;
751*75c67f9bSDoug Lyons WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
752*75c67f9bSDoug Lyons
753*75c67f9bSDoug Lyons /* Try to get an icon by walking the files Resource Section */
754*75c67f9bSDoug Lyons if (iconresdir->NumberOfIdEntries > 0)
755*75c67f9bSDoug Lyons {
756*75c67f9bSDoug Lyons xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)
757*75c67f9bSDoug Lyons (ULONG_PTR)(iconresdir + 1);
758*75c67f9bSDoug Lyons }
759*75c67f9bSDoug Lyons else
760*75c67f9bSDoug Lyons {
761*75c67f9bSDoug Lyons RetPtr[i] = 0;
762*75c67f9bSDoug Lyons continue;
763*75c67f9bSDoug Lyons }
764*75c67f9bSDoug Lyons
765*75c67f9bSDoug Lyons /* Get the Resource Directory */
766*75c67f9bSDoug Lyons resdir = (const IMAGE_RESOURCE_DIRECTORY *)
767*75c67f9bSDoug Lyons ((const char *)rootresdir + xresent->OffsetToDirectory);
768*75c67f9bSDoug Lyons
769*75c67f9bSDoug Lyons if (resdir->NumberOfIdEntries > 0) // Do we have entries
770*75c67f9bSDoug Lyons {
771*75c67f9bSDoug Lyons xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)
772*75c67f9bSDoug Lyons (ULONG_PTR)(resdir + 1);
773*75c67f9bSDoug Lyons }
774*75c67f9bSDoug Lyons else
775*75c67f9bSDoug Lyons {
776*75c67f9bSDoug Lyons RetPtr[i] = 0;
777*75c67f9bSDoug Lyons continue;
778*75c67f9bSDoug Lyons }
779*75c67f9bSDoug Lyons
780*75c67f9bSDoug Lyons /* Retrieve the data entry and find its address */
781*75c67f9bSDoug Lyons igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)
782*75c67f9bSDoug Lyons ((const char *)rootresdir + xresent->OffsetToData);
783*75c67f9bSDoug Lyons idata = RtlImageRvaToVa(RtlImageNtHeader((HMODULE)peimage),
784*75c67f9bSDoug Lyons (HMODULE)peimage, igdataent->OffsetToData, NULL);
785*75c67f9bSDoug Lyons if (!idata)
786*75c67f9bSDoug Lyons {
787*75c67f9bSDoug Lyons RetPtr[i] = 0;
788*75c67f9bSDoug Lyons continue;
789*75c67f9bSDoug Lyons }
790*75c67f9bSDoug Lyons
791*75c67f9bSDoug Lyons /* Check to see if this looks like an icon bitmap */
792*75c67f9bSDoug Lyons if (idata[0] == sizeof(BITMAPINFOHEADER))
793*75c67f9bSDoug Lyons {
794*75c67f9bSDoug Lyons BITMAPINFOHEADER bmih;
795*75c67f9bSDoug Lyons RtlCopyMemory(&bmih, idata, sizeof(BITMAPINFOHEADER));
796*75c67f9bSDoug Lyons /* Do the Width and Height look correct for an icon */
797*75c67f9bSDoug Lyons if ((bmih.biWidth * 2) == bmih.biHeight)
798*75c67f9bSDoug Lyons {
799*75c67f9bSDoug Lyons RetPtr[0] = CreateIconFromResourceEx(idata, igdataent->Size,
800*75c67f9bSDoug Lyons TRUE, 0x00030000, cx1, cy1, flags);
801*75c67f9bSDoug Lyons if (cx2 && cy2)
802*75c67f9bSDoug Lyons RetPtr[1] = CreateIconFromResourceEx(idata, idataent->Size,
803*75c67f9bSDoug Lyons TRUE, 0x00030000, cx2, cy2, flags);
804*75c67f9bSDoug Lyons ret = 1; // Set number of icons found
805*75c67f9bSDoug Lyons goto end; // Success so Exit
806*75c67f9bSDoug Lyons }
807*75c67f9bSDoug Lyons }
808*75c67f9bSDoug Lyons else
809*75c67f9bSDoug Lyons {
810*75c67f9bSDoug Lyons RetPtr[i] = 0;
811*75c67f9bSDoug Lyons continue;
812*75c67f9bSDoug Lyons }
813*75c67f9bSDoug Lyons #else
814c2c66affSColin Finck WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
815c2c66affSColin Finck RetPtr[i]=0;
816c2c66affSColin Finck continue;
817*75c67f9bSDoug Lyons #endif
818c2c66affSColin Finck }
819c2c66affSColin Finck xresdir = find_entry_default(xresdir, rootresdir);
820c2c66affSColin Finck idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
821c2c66affSColin Finck
822c2c66affSColin Finck idata = RtlImageRvaToVa(RtlImageNtHeader((HMODULE)peimage), (HMODULE)peimage, idataent->OffsetToData, NULL);
823c2c66affSColin Finck if (!idata)
824c2c66affSColin Finck {
825c2c66affSColin Finck WARN("no matching real address found for icondata!\n");
826c2c66affSColin Finck RetPtr[i]=0;
827c2c66affSColin Finck continue;
828c2c66affSColin Finck }
829c2c66affSColin Finck RetPtr[i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx1, cy1, flags);
830c2c66affSColin Finck if (cx2 && cy2)
831c2c66affSColin Finck RetPtr[++i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx2, cy2, flags);
832c2c66affSColin Finck }
833c2c66affSColin Finck ret = i; /* return number of retrieved icons */
834c2c66affSColin Finck } /* if(sig == IMAGE_NT_SIGNATURE) */
835c2c66affSColin Finck
836c2c66affSColin Finck end:
837c2c66affSColin Finck UnmapViewOfFile(peimage); /* success */
838c2c66affSColin Finck return ret;
839c2c66affSColin Finck }
840c2c66affSColin Finck
841c2c66affSColin Finck /***********************************************************************
842c2c66affSColin Finck * PrivateExtractIconsW [USER32.@]
843c2c66affSColin Finck *
844c2c66affSColin Finck * NOTES
845c2c66affSColin Finck * If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
846c2c66affSColin Finck * returned, with the LOWORD size icon first and the HIWORD size icon
847c2c66affSColin Finck * second.
848c2c66affSColin Finck * Also the Windows equivalent does extract icons in a strange way if
849c2c66affSColin Finck * nIndex is negative. Our implementation treats a negative nIndex as
850c2c66affSColin Finck * looking for that resource identifier for the first icon to retrieve.
851c2c66affSColin Finck *
852c2c66affSColin Finck * FIXME:
853c2c66affSColin Finck * should also support 16 bit EXE + DLLs, cursor and animated cursor as
854c2c66affSColin Finck * well as bitmap files.
855c2c66affSColin Finck */
856c2c66affSColin Finck
PrivateExtractIconsW(LPCWSTR lpwstrFile,int nIndex,int sizeX,int sizeY,HICON * phicon,UINT * pIconId,UINT nIcons,UINT flags)857c2c66affSColin Finck UINT WINAPI PrivateExtractIconsW (
858c2c66affSColin Finck LPCWSTR lpwstrFile,
859c2c66affSColin Finck int nIndex,
860c2c66affSColin Finck int sizeX,
861c2c66affSColin Finck int sizeY,
862c2c66affSColin Finck HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
863c2c66affSColin Finck UINT* pIconId, /* [out] pointer to array of nIcons icon identifiers or NULL */
864c2c66affSColin Finck UINT nIcons, /* [in] number of icons to retrieve */
865c2c66affSColin Finck UINT flags ) /* [in] LR_* flags used by LoadImage */
866c2c66affSColin Finck {
867c2c66affSColin Finck TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
868c2c66affSColin Finck debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
869c2c66affSColin Finck
870c2c66affSColin Finck if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
871c2c66affSColin Finck {
872c2c66affSColin Finck WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
873c2c66affSColin Finck }
8748a7b5a90SDoug Lyons #ifdef __REACTOS__
8758a7b5a90SDoug Lyons return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY,
8768a7b5a90SDoug Lyons pIconId, flags, TRUE);
8778a7b5a90SDoug Lyons #else
878c2c66affSColin Finck return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
8798a7b5a90SDoug Lyons #endif
880c2c66affSColin Finck }
881c2c66affSColin Finck
882c2c66affSColin Finck /***********************************************************************
883c2c66affSColin Finck * PrivateExtractIconsA [USER32.@]
884c2c66affSColin Finck */
885c2c66affSColin Finck
PrivateExtractIconsA(LPCSTR lpstrFile,int nIndex,int sizeX,int sizeY,HICON * phicon,UINT * piconid,UINT nIcons,UINT flags)886c2c66affSColin Finck UINT WINAPI PrivateExtractIconsA (
887c2c66affSColin Finck LPCSTR lpstrFile,
888c2c66affSColin Finck int nIndex,
889c2c66affSColin Finck int sizeX,
890c2c66affSColin Finck int sizeY,
891c2c66affSColin Finck HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
892c2c66affSColin Finck UINT* piconid, /* [out] pointer to array of nIcons icon identifiers or NULL */
893c2c66affSColin Finck UINT nIcons, /* [in] number of icons to retrieve */
894c2c66affSColin Finck UINT flags ) /* [in] LR_* flags used by LoadImage */
895c2c66affSColin Finck {
896c2c66affSColin Finck UINT ret;
897c2c66affSColin Finck INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
898c2c66affSColin Finck LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
899c2c66affSColin Finck #ifdef __REACTOS__
900c2c66affSColin Finck if (lpwstrFile == NULL)
901c2c66affSColin Finck return 0;
902c2c66affSColin Finck #endif
903c2c66affSColin Finck
904c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
905c2c66affSColin Finck ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
906c2c66affSColin Finck
907c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, lpwstrFile);
908c2c66affSColin Finck return ret;
909c2c66affSColin Finck }
910c2c66affSColin Finck
911c2c66affSColin Finck /***********************************************************************
912c2c66affSColin Finck * PrivateExtractIconExW [USER32.@]
913c2c66affSColin Finck * NOTES
914c2c66affSColin Finck * if nIndex == -1 it returns the number of icons in any case !!!
915c2c66affSColin Finck */
PrivateExtractIconExW(LPCWSTR lpwstrFile,int nIndex,HICON * phIconLarge,HICON * phIconSmall,UINT nIcons)916c2c66affSColin Finck UINT WINAPI PrivateExtractIconExW (
917c2c66affSColin Finck LPCWSTR lpwstrFile,
918c2c66affSColin Finck int nIndex,
919c2c66affSColin Finck HICON * phIconLarge,
920c2c66affSColin Finck HICON * phIconSmall,
921c2c66affSColin Finck UINT nIcons )
922c2c66affSColin Finck {
923c2c66affSColin Finck DWORD cyicon, cysmicon, cxicon, cxsmicon;
924c2c66affSColin Finck UINT ret = 0;
925c2c66affSColin Finck
926c2c66affSColin Finck TRACE("%s %d %p %p %d\n",
927c2c66affSColin Finck debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
928c2c66affSColin Finck
9298a7b5a90SDoug Lyons #ifdef __REACTOS__
9308a7b5a90SDoug Lyons if (nIndex == -1 || (!phIconSmall && !phIconLarge))
9318a7b5a90SDoug Lyons /* get the number of icons */
9328a7b5a90SDoug Lyons return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL,
9338a7b5a90SDoug Lyons LR_DEFAULTCOLOR, FALSE);
9348a7b5a90SDoug Lyons #else
935c2c66affSColin Finck if (nIndex == -1)
936c2c66affSColin Finck /* get the number of icons */
937c2c66affSColin Finck return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
9388a7b5a90SDoug Lyons #endif
939c2c66affSColin Finck
940c2c66affSColin Finck if (nIcons == 1 && phIconSmall && phIconLarge)
941c2c66affSColin Finck {
942c2c66affSColin Finck HICON hIcon[2];
943c2c66affSColin Finck cxicon = GetSystemMetrics(SM_CXICON);
944c2c66affSColin Finck cyicon = GetSystemMetrics(SM_CYICON);
945c2c66affSColin Finck cxsmicon = GetSystemMetrics(SM_CXSMICON);
946c2c66affSColin Finck cysmicon = GetSystemMetrics(SM_CYSMICON);
947c2c66affSColin Finck
9488a7b5a90SDoug Lyons #ifdef __REACTOS__
9498a7b5a90SDoug Lyons ret = ICO_ExtractIconExW(lpwstrFile, hIcon, nIndex, 2,
9508a7b5a90SDoug Lyons cxicon | (cxsmicon<<16),
9518a7b5a90SDoug Lyons cyicon | (cysmicon<<16), NULL,
9528a7b5a90SDoug Lyons LR_DEFAULTCOLOR, FALSE);
9538a7b5a90SDoug Lyons #else
954c2c66affSColin Finck ret = ICO_ExtractIconExW(lpwstrFile, hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
955c2c66affSColin Finck cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
9568a7b5a90SDoug Lyons #endif
957c2c66affSColin Finck *phIconLarge = hIcon[0];
958c2c66affSColin Finck *phIconSmall = hIcon[1];
959c2c66affSColin Finck return ret;
960c2c66affSColin Finck }
961c2c66affSColin Finck
962c2c66affSColin Finck if (phIconSmall)
963c2c66affSColin Finck {
964c2c66affSColin Finck /* extract n small icons */
965c2c66affSColin Finck cxsmicon = GetSystemMetrics(SM_CXSMICON);
966c2c66affSColin Finck cysmicon = GetSystemMetrics(SM_CYSMICON);
9678a7b5a90SDoug Lyons #ifdef __REACTOS__
9688a7b5a90SDoug Lyons ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
9698a7b5a90SDoug Lyons cysmicon, NULL, LR_DEFAULTCOLOR, FALSE);
9708a7b5a90SDoug Lyons #else
971c2c66affSColin Finck ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
972c2c66affSColin Finck cysmicon, NULL, LR_DEFAULTCOLOR);
9738a7b5a90SDoug Lyons #endif
974c2c66affSColin Finck }
975c2c66affSColin Finck if (phIconLarge)
976c2c66affSColin Finck {
977c2c66affSColin Finck /* extract n large icons */
978c2c66affSColin Finck cxicon = GetSystemMetrics(SM_CXICON);
979c2c66affSColin Finck cyicon = GetSystemMetrics(SM_CYICON);
9808a7b5a90SDoug Lyons #ifdef __REACTOS__
9818a7b5a90SDoug Lyons ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
9828a7b5a90SDoug Lyons cyicon, NULL, LR_DEFAULTCOLOR, FALSE);
9838a7b5a90SDoug Lyons #else
984c2c66affSColin Finck ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
985c2c66affSColin Finck cyicon, NULL, LR_DEFAULTCOLOR);
9868a7b5a90SDoug Lyons #endif
987c2c66affSColin Finck }
988c2c66affSColin Finck return ret;
989c2c66affSColin Finck }
990c2c66affSColin Finck
991c2c66affSColin Finck /***********************************************************************
992c2c66affSColin Finck * PrivateExtractIconExA [USER32.@]
993c2c66affSColin Finck */
PrivateExtractIconExA(LPCSTR lpstrFile,int nIndex,HICON * phIconLarge,HICON * phIconSmall,UINT nIcons)994c2c66affSColin Finck UINT WINAPI PrivateExtractIconExA (
995c2c66affSColin Finck LPCSTR lpstrFile,
996c2c66affSColin Finck int nIndex,
997c2c66affSColin Finck HICON * phIconLarge,
998c2c66affSColin Finck HICON * phIconSmall,
999c2c66affSColin Finck UINT nIcons )
1000c2c66affSColin Finck {
1001c2c66affSColin Finck UINT ret;
1002c2c66affSColin Finck INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
1003c2c66affSColin Finck LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1004c2c66affSColin Finck #ifdef __REACTOS__
1005c2c66affSColin Finck if (lpwstrFile == NULL)
1006c2c66affSColin Finck return 0;
1007c2c66affSColin Finck #endif
1008c2c66affSColin Finck
1009c2c66affSColin Finck TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
1010c2c66affSColin Finck
1011c2c66affSColin Finck MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
1012c2c66affSColin Finck ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
1013c2c66affSColin Finck HeapFree(GetProcessHeap(), 0, lpwstrFile);
1014c2c66affSColin Finck return ret;
1015c2c66affSColin Finck }
1016