Log in

View Full Version : RVA to file offset in C++??


Leeoniya
November 25th, 2003, 02:07
need help converting RVA to file offset in C++, i am relatively new to c++ so code snippets and explanations would help.

thanx
Leon

Leeoniya
November 25th, 2003, 04:19
trying to use PElib.dll, except the functions it exports are not __stdcall, as declared in the provided header. looking at the exports in Depends, shows the function name, but not @<# of args>. like you would see in a typical dll. the dll source is all asm, and i dunno how to rebuild it to make it work.

i also read about pushing the stack pointer by tallying up the size of each arg with __cdecl, but i'm not sure how to make that work either.

if anyone can help me rebuild it to useable format, that would be cool.

u can find the dll, header and asm source on

http://win32assembly.online.fr/source1.html

thanks.

cyberheg
November 25th, 2003, 04:21
Here is some code I wrote a few years ago.
It's a part of a class and some of the variables used in the code are member data of the class.
To use it you will need to do a bit of editing but it should be possible to fix in like 10 min (atleast it would be like that for me ).
The code needs windows.h included.

If you don't understand something then either go read a C++ book, msdn or a PE document. After that you can ask for questions

Note that the webforum removed all ident so it makes it a little harder to read.

-- CyberHeg

/*----------------------------------------------------------*
*
* RVAToOffset: Convert value from RVA to file offset.
*----------------------------------------------------------*/
DWORD CPE::RVAToOffset(DWORD dwRVA)
{
int i;
WORD wSections;
PIMAGE_SECTION_HEADER pSectionHdr;

/* Map first section */
pSectionHdr = IMAGE_FIRST_SECTION(m_pNtHdr);
wSections = GetNumberOfSections();

for (i = 0; i < wSections; i++)
{
if (pSectionHdr->VirtualAddress <= dwRVA)
if ((pSectionHdr->VirtualAddress + pSectionHdr->Misc.VirtualSize) > dwRVA)
{
dwRVA -= pSectionHdr->VirtualAddress;
dwRVA += pSectionHdr->PointerToRawData;
return (dwRVA);
}
pSectionHdr++;
}
return (-1);
}

/*----------------------------------------------------------*
*
* OffsetToRVA: Convert value from file offset to RVA.
*----------------------------------------------------------*/
DWORD CPE::OffsetToRVA(DWORD dwOffset)
{
int i;
WORD wSections;
PIMAGE_SECTION_HEADER pSectionHdr;

/* Map first section */
pSectionHdr = IMAGE_FIRST_SECTION(m_pNtHdr);
wSections = GetNumberOfSections();

for (i = 0; i < wSections; i++)
{
if (pSectionHdr->PointerToRawData <= dwOffset)
if ((pSectionHdr->PointerToRawData + pSectionHdr->SizeOfRawData) > dwOffset)
{
dwOffset -= pSectionHdr->PointerToRawData;
dwOffset += pSectionHdr->VirtualAddress;
return (dwOffset);
}

pSectionHdr++;
}
return (-1);
}