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