Call PrivateExtractIcon in user32 instead of duplicating the code.

oldstable
Alexandre Julliard 2000-10-19 20:28:12 +00:00
parent 11805e0a4b
commit cc7abe1f06
2 changed files with 14 additions and 450 deletions

View File

@ -13,7 +13,6 @@
#include "wine/winbase16.h"
#include "neexe.h"
#include "cursoricon.h"
#include "module.h"
#include "heap.h"
#include "debugtools.h"
@ -24,447 +23,7 @@
#include "wine/undocshell.h"
#include "shlwapi.h"
DEFAULT_DEBUG_CHANNEL(shell)
#include "pshpack1.h"
typedef struct
{
BYTE bWidth; /* Width, in pixels, of the image */
BYTE bHeight; /* Height, in pixels, of the image */
BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
BYTE bReserved; /* Reserved ( must be 0) */
WORD wPlanes; /* Color Planes */
WORD wBitCount; /* Bits per pixel */
DWORD dwBytesInRes; /* How many bytes in this resource? */
DWORD dwImageOffset; /* Where in the file is this image? */
} icoICONDIRENTRY, *LPicoICONDIRENTRY;
typedef struct
{
WORD idReserved; /* Reserved (must be 0) */
WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
WORD idCount; /* How many images */
icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
} icoICONDIR, *LPicoICONDIR;
#include "poppack.h"
#if 0
static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
{
TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
}
static void dumpIcoDir ( LPicoICONDIR entry )
{
TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
}
#endif
/*************************************************************************
* SHELL_GetResourceTable
*/
static DWORD SHELL_GetResourceTable(HFILE hFile, LPBYTE *retptr)
{ IMAGE_DOS_HEADER mz_header;
char magic[4];
int size;
TRACE("0x%08x %p\n", hFile, retptr);
*retptr = NULL;
_llseek( hFile, 0, SEEK_SET );
if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
{ if (mz_header.e_cblp == 1) /* .ICO file ? */
{ *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
return 1;
}
else
return 0; /* failed */
}
_llseek( hFile, mz_header.e_lfanew, SEEK_SET );
if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
return 0;
_llseek( hFile, mz_header.e_lfanew, SEEK_SET);
if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
return IMAGE_NT_SIGNATURE;
if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
{ IMAGE_OS2_HEADER ne_header;
LPBYTE pTypeInfo = (LPBYTE)-1;
if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
return 0;
if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
return 0;
size = ne_header.ne_restab - ne_header.ne_rsrctab;
if( size > sizeof(NE_TYPEINFO) )
{ pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
if( pTypeInfo )
{ _llseek(hFile, mz_header.e_lfanew+ne_header.ne_rsrctab, SEEK_SET);
if( _lread( hFile, (char*)pTypeInfo, size) != size )
{ HeapFree( GetProcessHeap(), 0, pTypeInfo);
pTypeInfo = NULL;
}
}
}
*retptr = pTypeInfo;
return IMAGE_OS2_SIGNATURE;
}
return 0; /* failed */
}
/*************************************************************************
* SHELL_LoadResource
*/
static BYTE * SHELL_LoadResource( HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
{ BYTE* ptr;
TRACE("0x%08x %p 0x%08x\n", hFile, pNInfo, sizeShift);
*uSize = (DWORD)pNInfo->length << sizeShift;
if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
{ _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
_lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
return ptr;
}
return 0;
}
/*************************************************************************
* ICO_LoadIcon
*/
static BYTE * ICO_LoadIcon( HFILE hFile, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
{ BYTE* ptr;
TRACE("0x%08x %p\n", hFile, lpiIDE);
*uSize = lpiIDE->dwBytesInRes;
if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize)) )
{ _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
_lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
return ptr;
}
return 0;
}
/*************************************************************************
* ICO_GetIconDirectory
*
* Reads .ico file and build phony ICONDIR struct
* see http://www.microsoft.com/win32dev/ui/icons.htm
*/
#define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
#define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
static BYTE * ICO_GetIconDirectory( HFILE hFile, LPicoICONDIR* lplpiID, ULONG *uSize )
{ CURSORICONDIR lpcid; /* icon resource in resource-dir format */
LPicoICONDIR lpiID; /* icon resource in file format */
int i;
TRACE("0x%08x %p\n", hFile, lplpiID);
_llseek( hFile, 0, SEEK_SET );
if( _lread(hFile,(char*)&lpcid, HEADER_SIZE_FILE) != HEADER_SIZE_FILE )
return 0;
if( lpcid.idReserved || (lpcid.idType != 1) || (!lpcid.idCount) )
return 0;
i = lpcid.idCount * sizeof(icoICONDIRENTRY);
lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, HEADER_SIZE_FILE + i);
if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
{ CURSORICONDIR * lpID; /* icon resource in resource format */
*uSize = lpcid.idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
{
/* copy the header */
lpID->idReserved = lpiID->idReserved = 0;
lpID->idType = lpiID->idType = 1;
lpID->idCount = lpiID->idCount = lpcid.idCount;
/* copy the entrys */
for( i=0; i < lpiID->idCount; i++ )
{ memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpiID->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
lpID->idEntries[i].wResId = i;
}
*lplpiID = lpiID;
return (BYTE *)lpID;
}
}
/* fail */
HeapFree( GetProcessHeap(), 0, lpiID);
return 0;
}
/*************************************************************************
*
* returns
* failure:0; success: icon handle or nr of icons (nIconIndex-1)
*/
HICON WINAPI ICO_ExtractIconEx(LPCSTR lpszExeFileName, HICON * RetPtr, INT nIconIndex, UINT n, UINT cxDesired, UINT cyDesired )
{ HGLOBAL hRet = 0;
LPBYTE pData;
OFSTRUCT ofs;
DWORD sig;
HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
UINT16 iconDirCount = 0,iconCount = 0;
LPBYTE peimage;
HANDLE fmapping;
ULONG uSize;
TRACE("(file %s,start %d,extract %d\n", lpszExeFileName, nIconIndex, n);
if( hFile == HFILE_ERROR || (nIconIndex!=-1 && !n) )
return hRet;
sig = SHELL_GetResourceTable(hFile,&pData);
/* ico file */
if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
{ BYTE *pCIDir = 0;
NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
NE_NAMEINFO *pIconStorage = NULL;
NE_NAMEINFO *pIconDir = NULL;
LPicoICONDIR lpiID = NULL;
TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
if( pData == (BYTE*)-1 )
{ pCIDir = ICO_GetIconDirectory(hFile, &lpiID, &uSize); /* check for .ICO file */
if( pCIDir )
{ iconDirCount = 1; iconCount = lpiID->idCount;
TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
}
}
else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
{ if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
{ iconDirCount = pTInfo->count;
pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
TRACE("\tfound directory - %i icon families\n", iconDirCount);
}
if( pTInfo->type_id == NE_RSCTYPE_ICON )
{ iconCount = pTInfo->count;
pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
TRACE("\ttotal icons - %i\n", iconCount);
}
pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
}
if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
{ if( nIconIndex == (UINT16)-1 )
{ RetPtr[0] = iconDirCount;
}
else if( nIconIndex < iconDirCount )
{ UINT16 i, icon;
if( n > iconDirCount - nIconIndex )
n = iconDirCount - nIconIndex;
for( i = nIconIndex; i < nIconIndex + n; i++ )
{ /* .ICO files have only one icon directory */
if( lpiID == NULL ) /* *.ico */
pCIDir = SHELL_LoadResource( hFile, pIconDir + i, *(WORD*)pData, &uSize );
RetPtr[i-nIconIndex] = pLookupIconIdFromDirectoryEx( pCIDir, TRUE, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0);
HeapFree(GetProcessHeap(), 0, pCIDir);
}
for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
{ pCIDir = NULL;
if( lpiID )
{ pCIDir = ICO_LoadIcon( hFile, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
}
else
{ for( i = 0; i < iconCount; i++ )
{ if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
{ pCIDir = SHELL_LoadResource( hFile, pIconStorage + i,*(WORD*)pData, &uSize );
}
}
}
if( pCIDir )
{ RetPtr[icon-nIconIndex] = (HICON) pCreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
}
else
{ RetPtr[icon-nIconIndex] = 0;
}
}
}
}
if( lpiID )
HeapFree( GetProcessHeap(), 0, lpiID);
else
HeapFree( GetProcessHeap(), 0, pData);
}
/* end ico file */
/* exe/dll */
if( sig == IMAGE_NT_SIGNATURE)
{ LPBYTE idata,igdata;
PIMAGE_DOS_HEADER dheader;
PIMAGE_NT_HEADERS pe_header;
PIMAGE_SECTION_HEADER pe_sections;
PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
int i,j;
if ( !(fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
{ WARN("failed to create filemap.\n"); /* FIXME, INVALID_HANDLE_VALUE? */
goto end_2; /* failure */
}
if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
{ WARN("failed to mmap filemap.\n");
goto end_2; /* failure */
}
dheader = (PIMAGE_DOS_HEADER)peimage;
pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, SHELL_GetResourceTable checked that */
pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
rootresdir = NULL;
for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
{ if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
continue;
/* FIXME: doesn't work when the resources are not in a seperate section */
if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
{ rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
break;
}
}
if (!rootresdir)
{ WARN("haven't found section for resource directory.\n");
goto end_3; /* failure */
}
/* search the group icon dir*/
if (!(icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE)))
{ WARN("No Icongroupresourcedirectory!\n");
goto end_3; /* failure */
}
iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
/* number of icons requested */
if( nIconIndex == -1 )
{ hRet = iconDirCount;
goto end_3; /* success */
}
/* (nIconIndex < 0): extract the icon by resource id */
if( nIconIndex < 0 )
{
int n = 0;
int iId = abs(nIconIndex);
PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
while(n<iconDirCount && xprdeTmp)
{
if(xprdeTmp->u1.Id == iId)
{
nIconIndex = n;
break;
}
n++;
xprdeTmp++;
}
if (nIconIndex < 0)
{
WARN("resource id %d not found\n", iId);
goto end_3; /* failure */
}
}
/* check nIconIndex to be in range */
if (nIconIndex >= iconDirCount)
{
WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
goto end_3; /* failure */
}
xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1); /* caller just wanted the number of entries */
if( n > iconDirCount - nIconIndex ) /* assure we don't get too much ... */
{ n = iconDirCount - nIconIndex;
}
xresent = xresent+nIconIndex; /* starting from specified index ... */
for (i=0;i<n;i++,xresent++)
{ PIMAGE_RESOURCE_DIRECTORY resdir;
/* go down this resource entry, name */
resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
/* default language (0) */
resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
/* lookup address in mapped image for virtual address */
igdata = NULL;
for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
{ if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
continue;
if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
continue;
igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
}
if (!igdata)
{ WARN("no matching real address for icongroup!\n");
goto end_3; /* failure */
}
RetPtr[i] = (HICON)pLookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
}
if (!(iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE)))
{ WARN("No Iconresourcedirectory!\n");
goto end_3; /* failure */
}
for (i=0;i<n;i++)
{ PIMAGE_RESOURCE_DIRECTORY xresdir;
xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
idata = NULL;
/* map virtual to address in image */
for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
{ if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
continue;
if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
continue;
idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
}
if (!idata)
{ WARN("no matching real address found for icondata!\n");
RetPtr[i]=0;
continue;
}
RetPtr[i] = (HICON) pCreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
}
hRet = RetPtr[0]; /* return first icon */
goto end_3; /* sucess */
}
goto end_1; /* unknown filetype */
/* cleaning up (try & catch would be nicer:-) ) */
end_3: UnmapViewOfFile(peimage); /* success */
end_2: CloseHandle(fmapping);
end_1: _lclose( hFile);
return hRet;
}
DEFAULT_DEBUG_CHANNEL(shell);
/********************** THE ICON CACHE ********************************/
@ -549,9 +108,8 @@ static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
{ HICON hiconLarge=0;
HICON hiconSmall=0;
ICO_ExtractIconEx(sSourceFile, &hiconLarge, dwSourceIndex, 1, 32, 32 );
ICO_ExtractIconEx(sSourceFile, &hiconSmall, dwSourceIndex, 1, 16, 16 );
PrivateExtractIconsA( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
PrivateExtractIconsA( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
if ( !hiconLarge || !hiconSmall)
{
@ -833,11 +391,11 @@ HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLar
TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
if (nIconIndex==-1) /* Number of icons requested */
return ICO_ExtractIconEx(lpszFile, NULL, -1, 0, 0, 0 );
return PrivateExtractIconsA( lpszFile, -1, 0, 0, NULL, 0, 0, 0 );
if (phiconLarge)
{ ret = ICO_ExtractIconEx(lpszFile, phiconLarge, nIconIndex, nIcons, 32, 32 );
{
ret = PrivateExtractIconsA( lpszFile, nIconIndex, 32, 32, phiconLarge, 0, nIcons, 0 );
if ( nIcons==1)
{ ret = phiconLarge[0];
}
@ -848,7 +406,8 @@ HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLar
phiconSmall = &ret;
if (phiconSmall)
{ ret = ICO_ExtractIconEx(lpszFile, phiconSmall, nIconIndex, nIcons, 16, 16 );
{
ret = PrivateExtractIconsA( lpszFile, nIconIndex, 16, 16, phiconSmall, 0, nIcons, 0 );
if ( nIcons==1 )
{ ret = phiconSmall[0];
}

View File

@ -3761,9 +3761,14 @@ INT WINAPI wvsprintfA(LPSTR,LPCSTR,va_list);
INT WINAPI wvsprintfW(LPWSTR,LPCWSTR,va_list);
#define wvsprintf WINELIB_NAME_AW(wvsprintf)
/* Undocumented functions */
/* NOTE: This is SYSTEM.3, not USER.182, which is also named KillSystemTimer */
WORD WINAPI SYSTEM_KillSystemTimer( WORD );
HRESULT WINAPI PrivateExtractIconsA(LPCSTR,INT,DWORD,DWORD,HICON*,DWORD,UINT,DWORD);
HRESULT WINAPI PrivateExtractIconsW(LPCWSTR,INT,DWORD,DWORD,HICON*,DWORD,UINT,DWORD);
/* Extra functions that don't exist in the Windows API */
HPEN WINAPI GetSysColorPen(INT);