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