Avoid going past the end of the relocation section. Skip sanity checks
[wine/multimedia.git] / dlls / user / exticon.c
blob9ebdf1355c268d75b1b0308f94bda1d14b4c547a
1 /*
2 * icon extracting
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 #include <string.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include "winbase.h"
12 #include "windef.h"
13 #include "winerror.h"
14 #include "wingdi.h"
15 #include "winuser.h"
16 #include "neexe.h"
17 #include "cursoricon.h"
18 #include "module.h"
19 #include "heap.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(icon)
24 #include "pshpack1.h"
26 typedef struct
28 BYTE bWidth; /* Width, in pixels, of the image */
29 BYTE bHeight; /* Height, in pixels, of the image */
30 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
31 BYTE bReserved; /* Reserved ( must be 0) */
32 WORD wPlanes; /* Color Planes */
33 WORD wBitCount; /* Bits per pixel */
34 DWORD dwBytesInRes; /* How many bytes in this resource? */
35 DWORD dwImageOffset; /* Where in the file is this image? */
36 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
38 typedef struct
40 WORD idReserved; /* Reserved (must be 0) */
41 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
42 WORD idCount; /* How many images */
43 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
44 } icoICONDIR, *LPicoICONDIR;
46 #include "poppack.h"
48 #if 0
49 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
51 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
52 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
53 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
54 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
56 static void dumpIcoDir ( LPicoICONDIR entry )
58 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
60 #endif
61 /*************************************************************************
62 * USER32_GetResourceTable
64 static DWORD USER32_GetResourceTable(LPBYTE peimage, LPBYTE *retptr)
66 IMAGE_DOS_HEADER * mz_header;
68 TRACE("%p %p\n", peimage, retptr);
70 *retptr = NULL;
72 mz_header = (IMAGE_DOS_HEADER*) peimage;
74 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
76 if (mz_header->e_cblp == 1) /* .ICO file ? */
78 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
79 return 1;
81 else
82 return 0; /* failed */
85 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
86 return IMAGE_NT_SIGNATURE;
88 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
90 IMAGE_OS2_HEADER * ne_header;
92 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
94 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
95 return 0;
97 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
98 *retptr = (LPBYTE)-1;
99 else
100 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
102 return IMAGE_OS2_SIGNATURE;
104 return 0; /* failed */
106 /*************************************************************************
107 * USER32_LoadResource
109 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
111 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
113 *uSize = (DWORD)pNInfo->length << sizeShift;
114 return peimage + ((DWORD)pNInfo->offset << sizeShift);
117 /*************************************************************************
118 * ICO_LoadIcon
120 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
122 TRACE("%p %p\n", peimage, lpiIDE);
124 *uSize = lpiIDE->dwBytesInRes;
125 return peimage + lpiIDE->dwImageOffset;
128 /*************************************************************************
129 * ICO_GetIconDirectory
131 * Reads .ico file and build phony ICONDIR struct
132 * see http://www.microsoft.com/win32dev/ui/icons.htm
134 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
135 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
137 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
139 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
140 CURSORICONDIR * lpID; /* icon resource in resource format */
141 int i;
143 TRACE("%p %p\n", peimage, lplpiID);
145 lpcid = (CURSORICONDIR*)peimage;
147 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
148 return 0;
150 /* allocate the phony ICONDIR structure */
151 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
152 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
154 /* copy the header */
155 lpID->idReserved = lpcid->idReserved;
156 lpID->idType = lpcid->idType;
157 lpID->idCount = lpcid->idCount;
159 /* copy the entrys */
160 for( i=0; i < lpcid->idCount; i++ )
162 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
163 lpID->idEntries[i].wResId = i;
166 *lplpiID = (LPicoICONDIR)peimage;
167 return (BYTE *)lpID;
169 return 0;
172 /*************************************************************************
173 * ICO_ExtractIconExW [internal]
175 * NOTES
176 * nIcons = 0: returns number of Icons in file
178 * returns
179 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
181 static HRESULT WINAPI ICO_ExtractIconExW(
182 LPCWSTR lpszExeFileName,
183 HICON * RetPtr,
184 INT nIconIndex,
185 UINT nIcons,
186 UINT cxDesired,
187 UINT cyDesired )
189 HGLOBAL hRet = E_FAIL;
190 LPBYTE pData;
191 DWORD sig;
192 HFILE hFile;
193 UINT16 iconDirCount = 0,iconCount = 0;
194 LPBYTE peimage;
195 HANDLE fmapping;
196 ULONG uSize;
198 TRACE("(file %s,start %d,extract %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons);
200 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, -1 );
201 if (hFile == INVALID_HANDLE_VALUE) return hRet;
203 /* Map the file */
204 fmapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
205 if (!fmapping)
207 WARN("CreateFileMapping error %ld\n", GetLastError() );
208 goto end_1;
211 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
213 WARN("MapViewOfFile error %ld\n", GetLastError() );
214 goto end_2;
218 sig = USER32_GetResourceTable(peimage,&pData);
220 /* ico file */
221 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
223 BYTE *pCIDir = 0;
224 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
225 NE_NAMEINFO *pIconStorage = NULL;
226 NE_NAMEINFO *pIconDir = NULL;
227 LPicoICONDIR lpiID = NULL;
229 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
231 if( pData == (BYTE*)-1 )
233 /* FIXME: pCIDir is allocated on the heap - memory leak */
234 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
235 if( pCIDir )
237 iconDirCount = 1; iconCount = lpiID->idCount;
238 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
241 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
243 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
245 iconDirCount = pTInfo->count;
246 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
247 TRACE("\tfound directory - %i icon families\n", iconDirCount);
249 if( pTInfo->type_id == NE_RSCTYPE_ICON )
251 iconCount = pTInfo->count;
252 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
253 TRACE("\ttotal icons - %i\n", iconCount);
255 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
258 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
260 if( nIcons == 0 )
262 hRet = iconDirCount;
264 else if( nIconIndex < iconDirCount )
266 UINT16 i, icon;
267 if( nIcons > iconDirCount - nIconIndex )
268 nIcons = iconDirCount - nIconIndex;
270 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
272 /* .ICO files have only one icon directory */
273 if( lpiID == NULL ) /* *.ico */
274 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
275 RetPtr[i-nIconIndex] = LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, 0);
278 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
280 pCIDir = NULL;
281 if( lpiID )
282 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
283 else
284 for( i = 0; i < iconCount; i++ )
285 if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
286 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
288 if( pCIDir )
289 RetPtr[icon-nIconIndex] = (HICON) CreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
290 else
291 RetPtr[icon-nIconIndex] = 0;
293 hRet = S_OK;
297 /* end ico file */
299 /* exe/dll */
300 else if( sig == IMAGE_NT_SIGNATURE )
302 LPBYTE idata,igdata;
303 PIMAGE_DOS_HEADER dheader;
304 PIMAGE_NT_HEADERS pe_header;
305 PIMAGE_SECTION_HEADER pe_sections;
306 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
307 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
308 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
309 int i,j;
311 dheader = (PIMAGE_DOS_HEADER)peimage;
312 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
313 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
314 rootresdir = NULL;
316 /* search for the root resource directory */
317 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
319 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
320 continue;
321 /* FIXME: doesn't work when the resources are not in a seperate section */
322 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
324 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
325 break;
329 if (!rootresdir)
331 WARN("haven't found section for resource directory.\n");
332 goto end_3; /* failure */
335 /* search for the group icon directory */
336 if (!(icongroupresdir = GetResDirEntryW(rootresdir, RT_GROUP_ICONW, (DWORD)rootresdir, FALSE)))
338 WARN("No Icongroupresourcedirectory!\n");
339 goto end_3; /* failure */
341 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
343 /* only number of icons requested */
344 if( nIcons == 0 )
346 hRet = iconDirCount;
347 goto end_3; /* success */
350 if( nIconIndex < 0 )
352 /* search resource id */
353 int n = 0;
354 int iId = abs(nIconIndex);
355 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
357 while(n<iconDirCount && xprdeTmp)
359 if(xprdeTmp->u1.Id == iId)
361 nIconIndex = n;
362 break;
364 n++;
365 xprdeTmp++;
367 if (nIconIndex < 0)
369 WARN("resource id %d not found\n", iId);
370 goto end_3; /* failure */
373 else
375 /* check nIconIndex to be in range */
376 if (nIconIndex >= iconDirCount)
378 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
379 goto end_3; /* failure */
383 /* assure we don't get too much */
384 if( nIcons > iconDirCount - nIconIndex )
385 nIcons = iconDirCount - nIconIndex;
387 /* starting from specified index */
388 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
390 for (i=0; i < nIcons; i++,xresent++)
392 PIMAGE_RESOURCE_DIRECTORY resdir;
394 /* go down this resource entry, name */
395 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
397 /* default language (0) */
398 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
399 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
401 /* lookup address in mapped image for virtual address */
402 igdata = NULL;
404 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
406 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
407 continue;
408 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
409 continue;
410 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
413 if (!igdata)
415 WARN("no matching real address for icongroup!\n");
416 goto end_3; /* failure */
418 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
421 if (!(iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE)))
423 WARN("No Iconresourcedirectory!\n");
424 goto end_3; /* failure */
427 for (i=0; i<nIcons; i++)
429 PIMAGE_RESOURCE_DIRECTORY xresdir;
430 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
431 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
432 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
433 idata = NULL;
435 /* map virtual to address in image */
436 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
438 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
439 continue;
440 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
441 continue;
442 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
444 if (!idata)
446 WARN("no matching real address found for icondata!\n");
447 RetPtr[i]=0;
448 continue;
450 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
452 hRet = S_OK; /* return first icon */
453 goto end_3; /* sucess */
454 } /* if(sig == IMAGE_NT_SIGNATURE) */
456 end_3: UnmapViewOfFile(peimage); /* success */
457 end_2: CloseHandle(fmapping);
458 end_1: _lclose( hFile);
459 return hRet;
462 /***********************************************************************
463 * PrivateExtractIconsW [USER32.@]
465 * NOTES
466 * nIndex = 1: a small and a large icon are extracted.
467 * the higher word of sizeXY contains the size of the small icon, the lower
468 * word the size of the big icon. phicon points to HICON[2].
470 * RETURNS
471 * nIcons > 0: HRESULT
472 * nIcons = 0: the number of icons
475 HRESULT WINAPI PrivateExtractIconsW (
476 LPCWSTR lpwstrFile,
477 int nIndex,
478 DWORD sizeX,
479 DWORD sizeY,
480 HICON * phicon, /* HICON* */
481 DWORD w, /* 0 */
482 UINT nIcons,
483 DWORD y ) /* 0x80 maybe LR_* constant */
485 DWORD ret;
486 TRACE("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx stub\n",
487 debugstr_w(lpwstrFile),nIndex, sizeX ,sizeY ,phicon,w,nIcons,y );
490 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
492 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff );
493 if (!SUCCEEDED(ret)) return ret;
494 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff );
496 else
498 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff );
501 FIXME_(icon)("hicon=%08x ret=0x%08lx\n", *phicon, ret);
503 return ret;
506 /***********************************************************************
507 * PrivateExtractIconsA [USER32.@]
510 HRESULT WINAPI PrivateExtractIconsA (
511 LPCSTR lpstrFile,
512 DWORD nIndex,
513 DWORD sizeX,
514 DWORD sizeY,
515 HICON * phicon,
516 DWORD w, /* 0 */
517 UINT nIcons,
518 DWORD y ) /* 0x80 */
520 DWORD ret;
521 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
523 FIXME_(icon)("%s 0x%08lx 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx stub\n",
524 lpstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y );
526 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y);
528 FIXME_(icon)("hicon=%08x ret=0x%08lx\n", *phicon, ret);
530 HeapFree(GetProcessHeap(), 0, lpwstrFile);
531 return ret;
534 /***********************************************************************
535 * PrivateExtractIconExW [USER32.443]
536 * NOTES
537 * if nIcons = -1 it returns the number of icons in any case !!!
539 HRESULT WINAPI PrivateExtractIconExW (
540 LPCWSTR lpwstrFile,
541 DWORD nIndex,
542 HICON * phIconLarge,
543 HICON * phIconSmall,
544 UINT nIcons )
546 DWORD cyicon, cysmicon, cxicon, cxsmicon;
547 HRESULT ret = 0;
549 TRACE("%s 0x%08lx %p %p 0x%08x\n",
550 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
552 if (nIndex == 1 && phIconSmall && phIconLarge)
554 HICON hIcon[2];
555 cxicon = GetSystemMetrics(SM_CXICON);
556 cyicon = GetSystemMetrics(SM_CYICON);
557 cxsmicon = GetSystemMetrics(SM_CXSMICON);
558 cysmicon = GetSystemMetrics(SM_CYSMICON);
560 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
561 (HICON*) &hIcon, 0, 2, 0 );
562 *phIconLarge = hIcon[0];
563 *phIconSmall = hIcon[1];
564 return ret;
567 if (nIndex != -1)
569 if (phIconSmall)
571 /* extract n small icons */
572 cxsmicon = GetSystemMetrics(SM_CXSMICON);
573 cysmicon = GetSystemMetrics(SM_CYSMICON);
574 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
576 if (phIconLarge )
578 /* extract n large icons */
579 cxicon = GetSystemMetrics(SM_CXICON);
580 cyicon = GetSystemMetrics(SM_CYICON);
581 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
583 return ret;
586 /* get the number of icons */
587 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
590 /***********************************************************************
591 * PrivateExtractIconExA [USER32.442]
593 HRESULT WINAPI PrivateExtractIconExA (
594 LPCSTR lpstrFile,
595 DWORD nIndex,
596 HICON * phIconLarge,
597 HICON * phIconSmall,
598 UINT nIcons )
600 DWORD ret;
601 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
603 TRACE("%s 0x%08lx %p %p 0x%08x\n",
604 lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
606 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
608 HeapFree(GetProcessHeap(), 0, lpwstrFile);
609 return ret;