📄 resource.c
字号:
found_type:
nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
if (HIWORD(resid) != 0) /* named resource */
{
BYTE len = strlen( resid );
for (count = typeInfo->count; count > 0; count--, nameInfo++)
{
BYTE *p = resTab + nameInfo->id;
if (nameInfo->id & 0x8000) continue;
if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
}
}
else /* numeric resource id */
{
WORD id = LOWORD(resid) | 0x8000;
for (count = typeInfo->count; count > 0; count--, nameInfo++)
if (nameInfo->id == id) goto found_name;
}
TRACE("No resid entry found for %p\n", typeid );
HeapFree( GetProcessHeap(), 0, resTab );
return FALSE;
found_name:
/* Return resource data */
if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
HeapFree( GetProcessHeap(), 0, resTab );
return TRUE;
}
#endif /* ! __REACTOS__ */
/***********************************************************************
* load_pe_resource [internal]
*/
static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
DWORD *resLen, DWORD *resOff )
{
IMAGE_NT_HEADERS pehd;
DWORD pehdoffset;
PIMAGE_DATA_DIRECTORY resDataDir;
PIMAGE_SECTION_HEADER sections;
LPBYTE resSection;
DWORD resSectionSize;
const void *resDir;
const IMAGE_RESOURCE_DIRECTORY *resPtr;
const IMAGE_RESOURCE_DATA_ENTRY *resData;
int i, nSections;
BOOL ret = FALSE;
/* Read in PE header */
pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
if ( !resDataDir->Size )
{
TRACE("No resources in PE dll\n" );
return FALSE;
}
/* Read in section table */
nSections = pehd.FileHeader.NumberOfSections;
sections = HeapAlloc( GetProcessHeap(), 0,
nSections * sizeof(IMAGE_SECTION_HEADER) );
if ( !sections ) return FALSE;
LZSeek( lzfd, pehdoffset +
sizeof(DWORD) + /* Signature */
sizeof(IMAGE_FILE_HEADER) +
pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
{
HeapFree( GetProcessHeap(), 0, sections );
return FALSE;
}
/* Find resource section */
for ( i = 0; i < nSections; i++ )
if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
&& resDataDir->VirtualAddress < sections[i].VirtualAddress +
sections[i].SizeOfRawData )
break;
if ( i == nSections )
{
HeapFree( GetProcessHeap(), 0, sections );
TRACE("Couldn't find resource section\n" );
return FALSE;
}
/* Read in resource section */
resSectionSize = sections[i].SizeOfRawData;
resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
if ( !resSection )
{
HeapFree( GetProcessHeap(), 0, sections );
return FALSE;
}
LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
/* Find resource */
resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
resPtr = (const IMAGE_RESOURCE_DIRECTORY*)resDir;
resPtr = find_entry_by_name( resPtr, typeid, resDir );
if ( !resPtr )
{
TRACE("No typeid entry found for %p\n", typeid );
goto done;
}
resPtr = find_entry_by_name( resPtr, resid, resDir );
if ( !resPtr )
{
TRACE("No resid entry found for %p\n", resid );
goto done;
}
resPtr = find_entry_default( resPtr, resDir );
if ( !resPtr )
{
TRACE("No default language entry found for %p\n", resid );
goto done;
}
/* Find resource data section */
resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
for ( i = 0; i < nSections; i++ )
if ( resData->OffsetToData >= sections[i].VirtualAddress
&& resData->OffsetToData < sections[i].VirtualAddress +
sections[i].SizeOfRawData )
break;
if ( i == nSections )
{
TRACE("Couldn't find resource data section\n" );
goto done;
}
/* Return resource data */
if ( resLen ) *resLen = resData->Size;
if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
+ sections[i].PointerToRawData;
ret = TRUE;
done:
HeapFree( GetProcessHeap(), 0, resSection );
HeapFree( GetProcessHeap(), 0, sections );
return ret;
}
/*************************************************************************
* GetFileResourceSize [VER.2]
*/
DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
LPCSTR lpszResId, LPDWORD lpdwFileOffset )
{
BOOL retv = FALSE;
HFILE lzfd;
OFSTRUCT ofs;
DWORD reslen;
TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
lpszResId );
lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
if ( lzfd < 0 ) return 0;
switch ( read_xx_header( lzfd ) )
{
case IMAGE_OS2_SIGNATURE:
#ifdef __REACTOS__
ERR("OS2 Images not supported under ReactOS at this time.");
#else
retv = find_ne_resource( lzfd, lpszResType, lpszResId,
&reslen, lpdwFileOffset );
#endif
break;
case IMAGE_NT_SIGNATURE:
retv = find_pe_resource( lzfd, lpszResType, lpszResId,
&reslen, lpdwFileOffset );
break;
}
LZClose( lzfd );
return retv? reslen : 0;
}
/*************************************************************************
* GetFileResource [VER.3]
*/
DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
LPCSTR lpszResId, DWORD dwFileOffset,
DWORD dwResLen, LPVOID lpvData )
{
BOOL retv = FALSE;
HFILE lzfd;
OFSTRUCT ofs;
DWORD reslen = dwResLen;
TRACE("(%s,type=%p,id=%p,off=%ld,len=%ld,data=%p)\n",
debugstr_a(lpszFileName), lpszResType, lpszResId,
dwFileOffset, dwResLen, lpvData );
lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
if ( lzfd < 0 ) return 0;
if ( !dwFileOffset )
{
switch ( read_xx_header( lzfd ) )
{
case IMAGE_OS2_SIGNATURE:
#ifdef __REACTOS__
ERR("OS2 Images not supported under ReactOS at this time.");
#else
retv = find_ne_resource( lzfd, lpszResType, lpszResId,
&reslen, &dwFileOffset );
#endif
break;
case IMAGE_NT_SIGNATURE:
retv = find_pe_resource( lzfd, lpszResType, lpszResId,
&reslen, &dwFileOffset );
break;
}
if ( !retv )
{
LZClose( lzfd );
return 0;
}
}
LZSeek( lzfd, dwFileOffset, SEEK_SET );
reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
LZClose( lzfd );
return reslen;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -