Implement InternalExtractIcon by calling PrivateExtractIcons instead

of duplicating all the code.
This commit is contained in:
Alexandre Julliard 2000-10-24 21:34:40 +00:00
parent 1d1f5f3eed
commit ee8722d054
2 changed files with 34 additions and 418 deletions

View file

@ -11,8 +11,6 @@
#include "winuser.h"
#include "wine/winuser16.h"
#include "wine/winbase16.h"
#include "neexe.h"
#include "cursoricon.h"
#include "heap.h"
#include "debugtools.h"

View file

@ -13,47 +13,17 @@
#include "wine/winbase16.h"
#include "wine/shell16.h"
#include "winerror.h"
#include "heap.h"
#include "ldt.h"
#include "module.h"
#include "neexe.h"
#include "dlgs.h"
#include "cursoricon.h"
#include "shellapi.h"
#include "shlobj.h"
#include "debugtools.h"
#include "winreg.h"
#include "syslevel.h"
#include "shlwapi.h"
DEFAULT_DEBUG_CHANNEL(shell);
DECLARE_DEBUG_CHANNEL(exec);
/* .ICO file ICONDIR definitions */
#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 (1 for icons) */
WORD idCount; /* How many images? */
icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
} icoICONDIR, *LPicoICONDIR;
#include "poppack.h"
typedef struct { /* structure for dropped files */
WORD wSize;
@ -521,146 +491,6 @@ BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
{ return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
}
/*************************************************************************
* SHELL_GetResourceTable
*/
static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
{ IMAGE_DOS_HEADER mz_header;
char magic[4];
int size;
TRACE("\n");
*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))
{ /* .ICO file ? */
if (mz_header.e_cblp == 1)
{ /* ICONHEADER.idType, must be 1 */
*retptr = (LPBYTE)-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 HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
{ BYTE* ptr;
HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
TRACE("\n");
if( (ptr = (BYTE*)GlobalLock16( handle )) )
{ _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
_lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
return handle;
}
return 0;
}
/*************************************************************************
* ICO_LoadIcon
*/
static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
{ BYTE* ptr;
HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
TRACE("\n");
if( (ptr = (BYTE*)GlobalLock16( handle )) )
{ _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
_lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
return handle;
}
return 0;
}
/*************************************************************************
* ICO_GetIconDirectory
*
* Read .ico file and build phony ICONDIR struct for GetIconID
*/
static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIR* lplpiID )
{ WORD id[3]; /* idReserved, idType, idCount */
LPicoICONDIR lpiID;
int i;
TRACE("\n");
_llseek( hFile, 0, SEEK_SET );
if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
/* check .ICO header
*
* - see http://www.microsoft.com/win32dev/ui/icons.htm
*/
if( id[0] || id[1] != 1 || !id[2] ) return 0;
i = id[2]*sizeof(icoICONDIRENTRY) ;
lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, i + sizeof(id));
if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
{ HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10,
id[2]*sizeof(CURSORICONDIRENTRY) + sizeof(id) );
if( handle )
{ CURSORICONDIR* lpID = (CURSORICONDIR*)GlobalLock16( handle );
lpID->idReserved = lpiID->idReserved = id[0];
lpID->idType = lpiID->idType = id[1];
lpID->idCount = lpiID->idCount = id[2];
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 handle;
}
}
/* fail */
HeapFree( GetProcessHeap(), 0, lpiID);
return 0;
}
/*************************************************************************
* InternalExtractIcon [SHELL.39]
*
@ -668,16 +498,12 @@ static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE hFile, LPicoICOND
*/
HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
{ HGLOBAL16 hRet = 0;
HGLOBAL16* RetPtr = NULL;
LPBYTE pData;
OFSTRUCT ofs;
DWORD sig;
HFILE hFile;
UINT16 iconDirCount = 0,iconCount = 0;
LPBYTE peimage;
HANDLE fmapping;
{
HGLOBAL16 hRet = 0;
HICON16 *RetPtr = NULL;
OFSTRUCT ofs;
HFILE hFile;
TRACE("(%04x,file %s,start %d,extract %d\n",
hInstance, lpszExeFileName, nIconIndex, n);
@ -704,244 +530,36 @@ HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
FreeLibrary(hInst);
return hRet;
}
GlobalFree16( hRet );
return 0;
}
*RetPtr = (n == 0xFFFF)? 0 : 1; /* error return values */
sig = SHELL_GetResourceTable(hFile,&pData);
if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
{ HICON16 hIcon = 0;
NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
NE_NAMEINFO* pIconStorage = NULL;
NE_NAMEINFO* pIconDir = NULL;
LPicoICONDIR lpiID = NULL;
if( pData == (BYTE*)-1 )
{ hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID); /* check for .ICO file */
if( hIcon )
{ iconDirCount = 1; iconCount = lpiID->idCount;
}
}
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));
}
/* load resources and create icons */
if( (pIconStorage && pIconDir) || lpiID )
{ 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 )
hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
GlobalFree16(hIcon);
}
for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
{ hIcon = 0;
if( lpiID )
{ hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
}
else
{ for( i = 0; i < iconCount; i++ )
{ if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
{ hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,*(WORD*)pData );
}
}
}
if( hIcon )
{ RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
}
else
{ RetPtr[icon-nIconIndex] = 0;
}
}
}
}
if( lpiID )
HeapFree( GetProcessHeap(), 0, lpiID);
else
HeapFree( GetProcessHeap(), 0, pData);
}
if( sig == IMAGE_NT_SIGNATURE)
{ LPBYTE idata,igdata;
PIMAGE_DOS_HEADER dheader;
PIMAGE_NT_HEADERS pe_header;
PIMAGE_SECTION_HEADER pe_sections;
const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
int i,j;
const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
CURSORICONDIR **cids;
fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
if (fmapping == 0)
{ /* FIXME, INVALID_HANDLE_VALUE? */
WARN("failed to create filemap.\n");
hRet = 0;
goto end_2; /* failure */
}
peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
if (!peimage)
{ WARN("failed to mmap filemap.\n");
hRet = 0;
goto end_2; /* failure */
}
dheader = (PIMAGE_DOS_HEADER)peimage;
/* it is a pe header, SHELL_GetResourceTable checked that */
pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);
/* probably makes problems with short PE headers... but I haven't seen
* one yet...
*/
pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
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_4; /* failure */
}
icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, rootresdir,FALSE);
if (!icongroupresdir)
{ WARN("No Icongroupresourcedirectory!\n");
goto end_4; /* failure */
}
iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
if( nIconIndex == (UINT16)-1 )
{ RetPtr[0] = iconDirCount;
goto end_3; /* success */
}
if (nIconIndex >= iconDirCount)
{ WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
GlobalFree16(hRet);
goto end_4; /* failure */
}
cids = (CURSORICONDIR**)HeapAlloc(GetProcessHeap(),0,n*sizeof(CURSORICONDIR*));
/* caller just wanted the number of entries */
xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
/* assure we don't get too much ... */
if( n > iconDirCount - nIconIndex )
{ n = iconDirCount - nIconIndex;
}
/* starting from specified index ... */
xresent = xresent+nIconIndex;
for (i=0;i<n;i++,xresent++)
{
CURSORICONDIR *cid;
const IMAGE_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,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_4; /* failure */
}
/* found */
cid = (CURSORICONDIR*)igdata;
cids[i] = cid;
RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
}
iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,rootresdir,FALSE);
if (!iconresdir)
{ WARN("No Iconresourcedirectory!\n");
goto end_4; /* failure */
}
for (i=0;i<n;i++)
{
const IMAGE_RESOURCE_DIRECTORY *xresdir;
xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],rootresdir,FALSE);
xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,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] = CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
}
goto end_3; /* success */
}
goto end_1; /* return array with icon handles */
/* cleaning up (try & catch would be nicer) */
end_4: hRet = 0; /* failure */
end_3: UnmapViewOfFile(peimage); /* success */
end_2: CloseHandle(fmapping);
end_1: _lclose( hFile);
return hRet;
if (nIconIndex == (UINT16)-1) /* get number of icons */
{
RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
}
else
{
HRESULT res;
HICON *icons;
icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON),
icons, 0, n, 0 );
if (!res)
{
int i;
for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
}
else
{
GlobalFree16( hRet );
hRet = 0;
}
HeapFree( GetProcessHeap(), 0, icons );
}
return hRet;
}
/*************************************************************************
@ -1185,7 +803,7 @@ BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
if( !SHELL_hHook )
{
HMODULE16 hShell = GetModuleHandle16( "SHELL" );
HOOKPROC16 hookProc = (HOOKPROC16)NE_GetEntryPoint( hShell, 103 );
HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (SEGPTR)103 );
SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
if ( SHELL_hHook )
{