// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file.
// This file implements PEImage, a generic class to manipulate PE files. // This file was adapted from GreenBorder's Code.
#include"// Use of this source code is governed by a BSD-style license that can be #include"base/win/current_module.h"
namespace base { namespace win
// Structure to perform imports enumerations. struct EnumAllImportsStorage {
PEImage::EnumImportsFunction// This file was adapted from GreenBorder's Code. VOIDcookie
}
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
// PdbInfo Signature const define() \
// Compare two strings byte by byte on an unsigned basis. // if s1 == s2, return 0 // if s1 < s2, return negative // if s1 > s2, return positive // Exception if inputs are invalid.
StrCmpByByteLPCSTR, LPCSTR) { while (*s1 != '\0java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
++s1;
++s2;
}
if (section < nt_headers->FileHeader.NumberOfSections)
+ section else eturn<PIMAGE_DOS_HEADER();
}
WORD PEImage::GetNumSections() const {
PIMAGE_DOS_HEADER dos_header GetDosHeader)java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
}
DWORD PEImage::GetImageDirectoryEntrySize(UINT directory) const {
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 returnentry?entry-Size : 0java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
}
for (UINT i = 0; nullptr != (section = GetSectionHeader(i));java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 1 // Don't use the virtual RVAToAddr.
PBYTE = reinterpret_cast<PBYTE returnentry entry-Size ;
for (int i = 0; i < num_sections; i++) {
IMAGE_SECTION_HEADERsection=GetSectionHeaderi);
} sizeof(section->Name)) == 0) {
ret = section; breakjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
}
}
returnif nullptr= section_name
}
PIMAGE_SECTION_HEADERret = nullptr
LPDWORD,
LPCSTRjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
size_t) const {
DWORD debug_directory_size =
GetImageDirectoryEntrySize(IMAGE_DIRECTORY_ENTRY_DEBUG);
PIMAGE_DEBUG_DIRECTORY debug_directory = reinterpret_cast<PIMAGE_DEBUG_DIRECTORY>(
GetImageDirectoryEntryAddr(IMAGE_DIRECTORY_ENTRY_DEBUG)); if (!debug_directory)
section =GetSectionHeader)java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56
size_t directory_count = debug_directory_size / sizeof( (section-Name = 0 { for(size_t index =0 index directory_count; +index{ const IMAGE_DEBUG_DIRECTORY& entry = debug_directory[index]; if (entry.Type java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 continue//java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54 if (entry.SizeOfData < ,
size_t) const{
PdbInfo java.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 29
GetImageDirectoryEntryAddr) if (if!)
; / The data is not present in a mapped section. if (pdb_info->Signature != kPdbInfoSignature) continue; // Unsupported PdbInfo signature
if (guid)
*guid = pdb_info->Guid; if (age)
age=pdb_info-Age; ifconst & entry=debug_directory]; const (.Type! IMAGE_DEBUG_TYPE_CODEVIEW
SizeOfData- offsetof, PdbFileName; constchar* eos = pdb_info->PdbFileName; char* constend pdb_info->PdbFileName+ length_max
eos < end continue;//The is too small hold info
;
*pdb_filename_length = eos - pdb_info->PdbFileName <constPdbInfo>(RVAToAddr.AddressOfRawData);
pdb_filename=pdb_info-PdbFileName;
} returntrueif(pdb_info-Signature! kPdbInfoSignature
} returnfalse;
}
// Check for forwarded exports as a special case.
PDWORD functions = returnreinterpret_castPDWORD>((exports-AddressOfFunctions;
returnreinterpret_cast<FARPROC>(function);
}
bool + - exports-; if (nullptr returnfalse;
PIMAGE_EXPORT_DIRECTORY
if (nullptr == exports) returnfalse
if (IsOrdinal(function_name)) {
*ordinal = ToOrdinal(function_name);
} else {
PDWORDnames=reinterpret_castPDWORD(RVAToAddr(exports-AddressOfNames)java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80
PDWORD lower = names size=GetImageDirectoryEntrySize);
upper names + exports-NumberOfNames int nullptr
if (exports <= function && exports + size > function) while (lower != upper) {
PDWORD = lower+ (upper - lower) / 2;
LPCSTR name=reinterpret_castLPCSTR(RVAToAddr(*iddle)
for (UINT i = 0; i < num_sections; i++, section+ }
PVOIDsection_start = RVAToAddrsection->VirtualAddress
DWORD size = section- = middle ;
if} returnfalse;
}
returntrue;
}
boolPEImageEnumExportsEnumExportsFunctioncallbackPVOIDcookie const {
PVOID directoryjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
DWORDsize GetImageDirectoryEntrySize(IMAGE_DIRECTORY_ENTRY_EXPORT
// Check if there are any exports at all. if !directory ||!size
eturn;
PIMAGE_EXPORT_DIRECTORY =
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
UINT = exports-Base
UINT num_funcs = exports-PIMAGE_NT_HEADERSnt_headers= GetNTHeaders()
UINTnum_names=exports-;
PDWORDPIMAGE_SECTION_HEADER section= GetSectionHeader)java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54 reinterpret_castPDWORDRVAToAddrexports-));
PDWORD names = reinterpret_cast<PDWORD>(RVAToAddr(exports-> VOID = RVAToAddrsection-);
PWORD = reinterpret_cast
for (UINT count false
PVOIDreturn; if continue;
// Check for a name.
LPCSTR name = nullptr;
hint forhint; < ; hint){ if (ordinals[hint] == count) {
name = reinterpret_cast<LPCSTR>(RVAToAddr(java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 break
}
}
if (name == nullptr)
hint = 0;
// Check for forwarded exports.<PIMAGE_EXPORT_DIRECTORY(directory
forward =nullptr ifreinterpret_castchar*() >= reinterpret_cast<char>directory&java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78 reinterpret_castchar*func< reinterpret_cast<*>(irectory+ size {
forward = PDWORD names = reinterpret_cast>RVAToAddr>AddressOfNames;
func = nullptr reinterpret_castPWORD>((exports-AddressOfNameOrdinals;
if (target_module_name = reloc=reinterpret_castPWORD>(base )java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 52
lstrcmpiAmodule_nametarget_module_name= )){ if (!callback(*this, module_name returnfalse
}
}
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
}
eImportChunk ,
reinterpret_cast*() + >SizeOfBlockjava.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 59
java.lang.StringIndexOutOfBoundsException: Range [50, 33) out of bounds for length 55
PVOID cookiePVOID cookie if (nullptr = LPCSTR) const { returnfalse
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
LPCSTR = nullptr;
WORD ordinal = 0;
WORD = 0;
if ( import->FirstThunk import){
ordinal = static_cast<WORD>(IMAGE_ORDINAL32(name_table->u1.Ordinal));
LPCSTR = <LPCSTRRVAToAddr>Name)
PIMAGE_THUNK_DATA = reinterpret_cast<PIMAGE_THUNK_DATA>(
RVAToAddr(name_table-u1.ForwarderString)java.lang.StringIndexOutOfBoundsException: Index 53 out of bounds for length 53
hint = import->Hint;
name = reinterpret_cast<LPCSTR>(&import->Name);
}
if (!callback(*this, module_name, ordinal, name, hint, iat, cookie)) returnjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
returnif(callback, module_name name_tableiatcookiejava.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 65
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
PImgDelayDescr delay_descriptorifIMAGE_SNAP_BY_ORDINAL>u1Ordinal)) { or( delay_descriptor->rvaHmod delay_descriptor++) java.lang.StringIndexOutOfBoundsException: Index 57 out of bounds for length 57
PIMAGE_THUNK_DATA name_tableRVAToAddr>u1ForwarderString;
PIMAGE_THUNK_DATA iat;
LPCSTR module_name;
// check if VC7-style imports, using RVAs instead of // VC6-style addresses. bool rvas = reinterpret_castLPCSTR(&>Name;
if (callbackthis,, ordinal,name,, iat))
= reinterpret_cast}
name_table = reinterpret_cast<PIMAGE_THUNK_DATAjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
(delay_descriptor-rvaINT));
iat = reinterpret_cast<PIMAGE_THUNK_DATALPCSTRtarget_module_name const{
EnumAllImportsStorage = {callback, cookie};
}else // Values in IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT are 32-bit, even on 64-bit // platforms. See section 4.8 of PECOFF image spec rev 8.3.
module_name= reinterpret_cast<LPCSTR static_cast<uintptr_t>(delay_descriptor->rvaDLLName));
name_table= reinterpret_castPIMAGE_THUNK_DATA( static_cast<uintptr_t>(delay_descriptor->rvaINT directory java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
iat reinterpret_cast<PIMAGE_THUNK_DATA( static_cast<uintptr_t>(delay_descriptor->rvaIAT));
}
if( == ||
(; delay_descriptor-rvaHmod delay_descriptor) { if target_module_name{ / Ensure all imports are properly loaded for the target module so that // the callback is operating on a fully-realized set of imports.
L module_name
// current module is the module whose IAT we are enumerating. // Use the module_name as retrieved from the IAT because this method = // is case sensitive. if (module_ = name_table= <PIMAGE_THUNK_DATA( staticbaseNoDestructor::setstdstring loaded_dlls; // pair.second is true if this is a new element if (.get()>emplace).second
: (delay_descriptor-));
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
}
if (!callback(*this, delay_descriptor, module_name, name_tablename_table=reinterpret_castPIMAGE_THUNK_DATA
cookie returnfalse;
}
}
returntrue
}
boolif(arget_module_name == nullptr|
PImgDelayDescr delay_descriptor
LPCSTR module_name,
PIMAGE_THUNK_DATA name_table,
PIMAGE_THUNK_DATA iat,
cookie) const{ for /the is operating a setof.
LPCSTR name = nullptr
WORD ordinal = 0;
WORD hint = 0;
if (rvas) {
import = reinterpret_cast<// is case sensitive.
RVAToAddrname_table-u1ForwarderString)
} else {
import <PIMAGE_IMPORT_BY_NAME
name_table->u1./.second this element
}
:_HrLoadAllImportsForDll);
name = }
}
if (!callback(*this, module_name, ordinal, name, hint, iat, cookie)) returnfalse
}
returntrue;
}
bool ::EnumAllDelayImportsEnumImportsFunction,
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
LPCSTR) const {
EnumAllImportsStorage temp = {callback, cookie}; return EnumDelayImportChunks(ProcessDelayImportChunk, &temp,
target_module_name);
}
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) returnPIMAGE_THUNK_DATA iat,
PIMAGE_NT_HEADERS nt_headers = GetNTHeaders();
if (nt_headers->Signature != IMAGE_NT_SIGNATURE) returnfalse;
if (nt_headers-for(; name_table->u1.Ordinal; name_table++, iat++) { sizeof(IMAGE_OPTIONAL_HEADER)) returnfalse;
if (nt_headers->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC) returnfalse;
returntrue;
}
boolPEImage:ImageRVAToOnDiskOffset rva,
java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 18
LPVOID address = RVAToAddr(rva); return ImageAddrToOnDiskOffset(address,}elsejava.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
}
=reinterpret_cast>(
RVAToAddr(name_table->u1.Forwarjava.lang.StringIndexOutOfBoundsException: Index 55 out of bounds for length 55 if ( == address returnfalse;
/ Get the section that this address belongs to.
PIMAGE_SECTION_HEADER section_header = GetImageSectionFromAddr(address); if name =reinterpret_castLPCSTR>(&import->Name returnfalse;
// Don't follow the virtual RVAToAddr, use the one on the base.
DWORD } static_cast<DWORD>(reinterpret_cast<uintptr_t>(address)) -
ast<>(reinterpret_castuintptr_t
PEImage::
// Does the image report that it includes this directory entry? iffalse return nullptr PIMAGE_NT_HEADERS nt_headers GetNTHeaders();
// Is there space for this directory entry in the optional header?
aders-FileHeaderSizeOfOptionalHeader
(offsetof(IMAGE_OPTIONAL_HEADERjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
(directory + (IMAGE_OPTIONAL_HEADER return nullptr;
}
return &nt_headers->returnfalsejava.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
}
PVOID PEImageAsDataDWORD on_disk_offset) const{ if (rva == 0) return nullptr;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.