function RVAToFileOffset(FileName:string; RVA: Cardinal): Cardinal;
var
  MemPE: TFileStream;
  PEDosHead: TImageDosHeader;
  PENtHead: TImageNtHeaders;
  Section : TImageSectionHeader;
  i, SectionsCount: Integer;
begin
  Result := RVA;
  MemPE:=TFileStream.Create(FileName,fmOpenReadWrite);
  try
    MemPE.Seek(0, soFromBeginning);
    MemPE.Read(PEDosHead, SizeOf(PEDosHead));
    MemPE.Seek(PEDosHead._lfanew, soFromBeginning);
    MemPE.Read(PENtHead, SizeOf(PENtHead));

SectionsCount := PENtHead.FileHeader.NumberOfSections;

if SectionsCount <> 0 then
      for i := 0 to SectionsCount - 1 do
      begin
        MemPE.Read(Section, SizeOf(Section));
        if (RVA >= Section.VirtualAddress) and (RVA < Section.VirtualAddress + Section.SizeOfRawData) then
        begin
          Result := RVA - Section.VirtualAddress + Section.PointerToRawData;
          Break;
        end;
      end;
  finally
    FreeAndNil(MemPE);
  end;
end;

function FileOffsetToRVA(FileName:string; Offset: Cardinal): Cardinal;
var
  MemPE: TFileStream;
  PEDosHead: TImageDosHeader;
  PENtHead: TImageNtHeaders;
  Section : TImageSectionHeader;
  i, SectionsCount: Integer;
begin 
  Result :=Offset;
  MemPE:=TFileStream.Create(FileName,fmOpenReadWrite);
  try
    MemPE.Seek(0, soFromBeginning);
    MemPE.Read(PEDosHead, SizeOf(PEDosHead));
    MemPE.Seek(PEDosHead._lfanew, soFromBeginning);
    MemPE.Read(PENtHead, SizeOf(PENtHead));

SectionsCount := PENtHead.FileHeader.NumberOfSections;

if SectionsCount <> 0 then
      for i := 0 to SectionsCount - 1 do
      begin
        MemPE.Read(Section, SizeOf(Section));
        if (Offset >= Section.PointerToRawData) and (Offset < Section.PointerToRawData + Section.SizeOfRawData) then
        begin
          Result := Offset - Section.PointerToRawData + Section.VirtualAddress;
          Break;
        end;
      end;
  finally
    FreeAndNil(MemPE);
  end;
end;

程序源代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Button2: TButton;
    Edit5: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function FileOffsetToRVA(FileName:string; Offset: Cardinal): Cardinal;
var
  MemPE: TFileStream;
  PEDosHead: TImageDosHeader;
  PENtHead: TImageNtHeaders;
  Section : TImageSectionHeader;
  i, SectionsCount: Integer;
begin
  Result :=Offset;
  MemPE:=TFileStream.Create(FileName,fmOpenReadWrite);
  try
    MemPE.Seek(0, soFromBeginning);
    MemPE.Read(PEDosHead, SizeOf(PEDosHead));
    MemPE.Seek(PEDosHead._lfanew, soFromBeginning);
    MemPE.Read(PENtHead, SizeOf(PENtHead));

SectionsCount := PENtHead.FileHeader.NumberOfSections;

if SectionsCount <> 0 then
      for i := 0 to SectionsCount - 1 do
      begin
        MemPE.Read(Section, SizeOf(Section));
        if (Offset >= Section.PointerToRawData) and (Offset < Section.PointerToRawData + Section.SizeOfRawData) then
        begin
          Result := Offset - Section.PointerToRawData + Section.VirtualAddress;
          Break;
        end;
      end;
  finally
    FreeAndNil(MemPE);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := string(FileOffsetToRVA(Edit5.Text,Cardinal(Edit1.Text)));
end;

function RVAToFileOffset(FileName:string; RVA: Cardinal): Cardinal;
var
  MemPE: TFileStream;
  PEDosHead: TImageDosHeader;
  PENtHead: TImageNtHeaders;
  Section : TImageSectionHeader;
  i, SectionsCount: Integer;
begin
  Result := RVA;
  MemPE:=TFileStream.Create(FileName,fmOpenReadWrite);
  try
    MemPE.Seek(0, soFromBeginning);
    MemPE.Read(PEDosHead, SizeOf(PEDosHead));
    MemPE.Seek(PEDosHead._lfanew, soFromBeginning);
    MemPE.Read(PENtHead, SizeOf(PENtHead));

SectionsCount := PENtHead.FileHeader.NumberOfSections;

if SectionsCount <> 0 then
      for i := 0 to SectionsCount - 1 do
      begin
        MemPE.Read(Section, SizeOf(Section));
        if (RVA >= Section.VirtualAddress) and (RVA < Section.VirtualAddress + Section.SizeOfRawData) then
        begin
          Result := RVA - Section.VirtualAddress + Section.PointerToRawData;
          Break;
        end;
      end;
  finally
    FreeAndNil(MemPE);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Edit4.Text := string(RVAToFileOffset(Edit5.Text,Cardinal(Edit3.Text)));
end;

end.

程序界面:

RVA与Offset的换算函数的更多相关文章

  1. excel中的单位换算函数convert()

    有时,我们在处理数据的时候,需要进行单位换算,比如“7小时24分”换算成小时,可以直接除以或乘以相应的进制来计算,但是在excel中,有一个convert()函数更加方便: 此函数属于工程函数,平时可 ...

  2. 【PE结构】由浅入深PE基础学习-菜鸟手动查询导出表、相对虚拟地址(RVA)与文件偏移地址转换(FOA)

    0 前言 此篇文章想写如何通过工具手查导出表.PE文件代码编程过程中的原理.文笔不是很好,内容也是查阅了很多的资料后整合出来的.希望借此加深对PE文件格式的理解,也希望可以对看雪论坛有所贡献.因为了解 ...

  3. RVA与RWA的关系

    RVA与RWA的关系 原理比较简单:首先判断这个地址是否在PE头中,如果在,文件偏移和内存偏移相等,如果存在于文件的区段中,则利用以下公式: 内存偏移 - 该段起始的RVA(VirtualAddres ...

  4. 【python cookbook】【数据结构与算法】19.同时对数据做转换和换算

    问题:我们需要调用一个换算函数(例如sum().min().max()),但是首先需对数据做转换或者筛选处理 解决方案:非常优雅的方法---在函数参数中使用生成器表达式 例如: # 计算平方和 num ...

  5. C语言中fseek函数

    C语言fseek()函数:用来设定文件的当前读写位置 头文件: #include <stdio.h> 定义函数: int fseek(FILE * stream, long offset, ...

  6. 【Linux C中文函数手册】文件内容控制函数

    文件内容控制函数 1)clearerr 清除文件流的错误旗标 相关函数 feof表头文件 #include<stdio.h>定义函数 void clearerr(FILE * stream ...

  7. C语言文件函数

    FILE *fp: 其中的FILE应该大写,它实际上是系统定义的一个结构,在stdio.h文件中.该结构中有文件名,文件状态,文件当前的读写信息等. fp是指向FILE结构的指针变量,通过fp可以找到 ...

  8. C语言文件操作函数

    C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * str ...

  9. fseek/ftell/rewind/fgetpos/fsetpos函数使用-linux

    程序: #include<stdio.h> int main(int argc,char *argv[]) { FILE * stream; fpos_t pos; stream = fo ...

随机推荐

  1. 一个ListView中显示不同的item(分组)

    MainActivity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import ...

  2. win7里开始菜单属性里的隐私项无法选择解决方法

    具体问题如下图,其中的隐私项目呈现灰色无法选择: 解决方法如下: win+R 输入gpedit.msc 回车,[用户配置][管理模板][开始菜单和任务栏],在右侧找到[关闭用户跟踪],双击进入设置界面 ...

  3. delphi的多线程编程

    多线程的基本概念 win 98/nt/2000/xp 是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用cpu 运行时间和资源,或者说,把cpu 时间划成片,每个片分给不同的线程 ...

  4. does not match bootstrap parameter

    问题描述: DBD::mysql object version 2.0419 does not match bootstrap parameter 2.0902 at /usr/libdata/per ...

  5. Ioc 控制反转 实例

    关于IOC 或者是DI 什么的真的很坑爹. 开始理解了这东西了然后闲的没事就又百度了一下,得  我又凌乱了.  看了两个大神的贴 尼玛啊 完全是反过来了. 纠结了半天.然后就想找个简单点不坑爹的原理代 ...

  6. Hashset,Iterator

    HashSet类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素. (一)HashSet的一些特性如下: 1.HashSet中的值不能重复 ...

  7. linux C 管道

    单一进程使用管道基本上毫无意义.管道一般用来子进程和父进程之间的通信,或者兄弟进程间的通信. 创建管道的主要函数是pipe #include<unistd.h> ]) pipe函数创建一个 ...

  8. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  9. linux之iptables总结

    netfilter/iptables概述:     netfilter/iptables是Linux内核防火墙架构,由netfilter实现功能,由iptables提供接口.     iptables ...

  10. Mysql 更改最大连接数

    方法一: 进入MySQL安装目录 打开MySQL配置文件 my.ini 或 my.cnf查找 max_connections=100 修改为 max_connections=1000 服务里重起MyS ...