PE合并节【内存对齐和文件对其大小不一样版本】

PVOID PE_Headers::CompressSection(IN PVOID File_Buffer)

{

        PVOID newbuff = NULL;

        PBYTE read = NULL, write = NULL;

        DWORD i;

       

        if (File_Buffer == NULL)

        {

                Error_Flag |= EMPTY_IMAGEBUFFER;

                return NULL;

        }

       

        //新生成的文件大小 = 最后节的RVA + 最后节的FOA - 头的RVA + 头的FOA

        File_Size = pSection_Header[pPE_Header->NumberOfSections - 1].VirtualAddress

                + pSection_Header[pPE_Header->NumberOfSections - 1].SizeOfRawData

                - pSection_Header->VirtualAddress + pSection_Header->PointerToRawData;               

        newbuff = malloc(File_Size);

        memset(newbuff, 0, File_Size);

       

        memcpy (newbuff, pDos_Header, pOptional_Header->SizeOfHeaders);                        //拷贝头部

        for (i = 0; i < pPE_Header->NumberOfSections; i++)

        {

                read = (PBYTE) pDos_Header + pSection_Header[i].PointerToRawData;

                write = (PBYTE) newbuff + pSection_Header[i].VirtualAddress

                        - pSection_Header->VirtualAddress + pSection_Header->PointerToRawData;        //因头部的RVA和FOA的不同产生的偏移量

                memcpy(write, read, pSection_Header[i].SizeOfRawData);                       

        }

       

        Release(File_Buffer);

        GetPEHeaders(newbuff);

       

        //合并后,virtualsize = 文件大小 - 头的文件大小

        pSection_Header->Misc.VirtualSize = File_Size - pSection_Header->PointerToRawData;

        pSection_Header->SizeOfRawData = File_Size - pSection_Header->PointerToRawData;

        for (i = 1; i < pPE_Header->NumberOfSections; i++)

        {

                pSection_Header->Characteristics |= pSection_Header[i].Characteristics;                //把每个节的节属性合并到第一个节里

                memset(&pSection_Header[i], 0, sizeof(IMAGE_SECTION_HEADER));                        //把原来节的数据置0,以便新增一个节

        }

        pPE_Header->NumberOfSections = 1;

        return newbuff;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值