mirror of
https://github.com/reactos/reactos.git
synced 2025-05-08 19:27:00 +00:00

- winnt.h: respect WIN32_NO_STATUS for DBG codes. - rtl: change STDCALL to NTAPI - everything else: add precompiled headers where missing, define WIN32_NO_STATUS. svn path=/trunk/; revision=18598
152 lines
3.2 KiB
C
152 lines
3.2 KiB
C
/* COPYRIGHT: See COPYING in the top level directory
|
|
* PROJECT: ReactOS system libraries
|
|
* FILE: lib/rtl/image.c
|
|
* PURPOSE: Image handling functions
|
|
* PROGRAMMER: Eric Kohl
|
|
*/
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
#include <rtl.h>
|
|
|
|
#define NDEBUG
|
|
#include <debug.h>
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
/*
|
|
* @implemented
|
|
*/
|
|
PIMAGE_NT_HEADERS NTAPI
|
|
RtlImageNtHeader (IN PVOID BaseAddress)
|
|
{
|
|
PIMAGE_NT_HEADERS NtHeader;
|
|
PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)BaseAddress;
|
|
|
|
if (DosHeader && DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
|
|
{
|
|
DPRINT1("DosHeader->e_magic %x\n", DosHeader->e_magic);
|
|
DPRINT1("NtHeader 0x%p\n", ((ULONG_PTR)BaseAddress + DosHeader->e_lfanew));
|
|
}
|
|
|
|
if (DosHeader && DosHeader->e_magic == IMAGE_DOS_SIGNATURE)
|
|
{
|
|
NtHeader = (PIMAGE_NT_HEADERS)((ULONG_PTR)BaseAddress + DosHeader->e_lfanew);
|
|
if (NtHeader->Signature == IMAGE_NT_SIGNATURE)
|
|
return NtHeader;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/*
|
|
* @implemented
|
|
*/
|
|
PVOID
|
|
NTAPI
|
|
RtlImageDirectoryEntryToData (
|
|
PVOID BaseAddress,
|
|
BOOLEAN bMappedAsImage,
|
|
ULONG Directory,
|
|
PULONG Size
|
|
)
|
|
{
|
|
PIMAGE_NT_HEADERS NtHeader;
|
|
ULONG Va;
|
|
|
|
/* Magic flag for non-mapped images. */
|
|
if ((ULONG_PTR)BaseAddress & 1)
|
|
{
|
|
BaseAddress = (PVOID)((ULONG_PTR)BaseAddress & ~1);
|
|
bMappedAsImage = FALSE;
|
|
}
|
|
|
|
|
|
NtHeader = RtlImageNtHeader (BaseAddress);
|
|
if (NtHeader == NULL)
|
|
return NULL;
|
|
|
|
if (Directory >= NtHeader->OptionalHeader.NumberOfRvaAndSizes)
|
|
return NULL;
|
|
|
|
Va = NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress;
|
|
if (Va == 0)
|
|
return NULL;
|
|
|
|
*Size = NtHeader->OptionalHeader.DataDirectory[Directory].Size;
|
|
|
|
if (bMappedAsImage || Va < NtHeader->OptionalHeader.SizeOfHeaders)
|
|
return (PVOID)((ULONG_PTR)BaseAddress + Va);
|
|
|
|
/* image mapped as ordinary file, we must find raw pointer */
|
|
return (PVOID)RtlImageRvaToVa (NtHeader, BaseAddress, Va, NULL);
|
|
}
|
|
|
|
|
|
/*
|
|
* @implemented
|
|
*/
|
|
PIMAGE_SECTION_HEADER
|
|
NTAPI
|
|
RtlImageRvaToSection (
|
|
PIMAGE_NT_HEADERS NtHeader,
|
|
PVOID BaseAddress,
|
|
ULONG Rva
|
|
)
|
|
{
|
|
PIMAGE_SECTION_HEADER Section;
|
|
ULONG Va;
|
|
ULONG Count;
|
|
|
|
Count = NtHeader->FileHeader.NumberOfSections;
|
|
Section = (PIMAGE_SECTION_HEADER)((ULONG)&NtHeader->OptionalHeader +
|
|
NtHeader->FileHeader.SizeOfOptionalHeader);
|
|
while (Count)
|
|
{
|
|
Va = Section->VirtualAddress;
|
|
if ((Va <= Rva) &&
|
|
(Rva < Va + Section->SizeOfRawData))
|
|
return Section;
|
|
Section++;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/*
|
|
* @implemented
|
|
*/
|
|
ULONG
|
|
NTAPI
|
|
RtlImageRvaToVa (
|
|
PIMAGE_NT_HEADERS NtHeader,
|
|
PVOID BaseAddress,
|
|
ULONG Rva,
|
|
PIMAGE_SECTION_HEADER *SectionHeader
|
|
)
|
|
{
|
|
PIMAGE_SECTION_HEADER Section = NULL;
|
|
|
|
if (SectionHeader)
|
|
Section = *SectionHeader;
|
|
|
|
if (Section == NULL ||
|
|
Rva < Section->VirtualAddress ||
|
|
Rva >= Section->VirtualAddress + Section->SizeOfRawData)
|
|
{
|
|
Section = RtlImageRvaToSection (NtHeader, BaseAddress, Rva);
|
|
if (Section == NULL)
|
|
return 0;
|
|
|
|
if (SectionHeader)
|
|
*SectionHeader = Section;
|
|
}
|
|
|
|
return (ULONG)((ULONG_PTR)BaseAddress +
|
|
Rva +
|
|
Section->PointerToRawData -
|
|
(ULONG_PTR)Section->VirtualAddress);
|
|
}
|
|
|
|
/* EOF */
|