⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageloader.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <windows.h>
#include <winnt.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc.h"
#include "From/ARM/ARM.h"
#include "From/m68k/m68k.h"
#include "From/PPC/PPC.h"

static  CPU_INT machine_type = 0;
//static  CPU_INT ToMachine_type = IMAGE_FILE_MACHINE_I386;
static  CPU_INT ToMachine_type = IMAGE_FILE_MACHINE_POWERPC;
/*
 * infileName       file name to convert or disambler 
 * outputfileName   file name to save to
 * BaseAddress      the address we should emulate
 * cpuid            the cpu we choice not vaild for pe loader
 * type             the loading mode Auto, PE, bin
 * mode             disambler mode : 0 the arch cpu.
 *                  translate mode : 1 intel
 *                  translate mode : 2 ppc
 * 
 */

static void SetCPU(CPU_INT FromCpu, CPU_INT mode)
{
    machine_type = FromCpu;
    switch(mode)
    {
        case 0:
            ToMachine_type = machine_type;
            break;

        case 1:
            ToMachine_type = IMAGE_FILE_MACHINE_I386;
            break;

        case 2:
            ToMachine_type = IMAGE_FILE_MACHINE_POWERPC;
            break;

        default:
            printf("Not supported mode\n");
            break;

    }
}

static void Convert(FILE *outfp, CPU_INT FromCpu, CPU_INT mode)
{
    SetCPU(machine_type,mode);
    AnyalsingProcess();
    ConvertProcess(outfp, machine_type, ToMachine_type);
    FreeAny();
}


CPU_INT LoadPFileImage( char *infileName, char *outputfileName, 
                     CPU_UNINT BaseAddress, char *cpuid,
                     CPU_UNINT type, CPU_INT mode)
{
    FILE *infp;
    FILE *outfp;
    CPU_BYTE *cpu_buffer;   
    CPU_UNINT cpu_pos = 0;
    CPU_UNINT cpu_size=0;
    CPU_INT ret;
     //fopen("testms.exe","RB");
    

    /* Open file for read */

    if (!(infp = fopen(infileName, "rb")))
    {
        printf("Can not open file %s\n",infileName);
        return 3;
    }

    /* Open file for write */
    if (!(outfp = fopen(outputfileName,"wb")))
    {
        printf("Can not open file %s\n",outputfileName);
        return 4;
    }

    /* Load the binary file to a memory buffer */
    fseek(infp,0,SEEK_END);
    if (ferror(infp))
    {
        printf("error can not seek in the read file");
        fclose(infp);
        fclose(outfp);
        return 5;
    }
    
    /* get the memory size buffer */
    cpu_size = ftell(infp);
    if (ferror(infp))
    {
        printf("error can not get file size of the read file");
        fclose(infp);
        fclose(outfp);
        return 6;
    }

    /* Load the binary file to a memory buffer */
    fseek(infp,0,SEEK_SET);
    if (ferror(infp))
    {
        printf("error can not seek in the read file");
        fclose(infp);
        fclose(outfp);
        return 5;
    }

    if (cpu_size==0)
    {
        printf("error file size is Zero lenght of the read file");
        fclose(infp);
        fclose(outfp);
        return 7;
    }

    /* alloc memory now */
   ;
    if (!(cpu_buffer = (unsigned char *) malloc(cpu_size+1)))
    {
        printf("error can not alloc %uld size for memory buffer",cpu_size);
        fclose(infp);
        fclose(outfp);
        return 8;
    }
    ZeroMemory(cpu_buffer,cpu_size);

    /* read from the file now in one sweep */
    fread((void *)cpu_buffer,1,cpu_size,infp);
    if (ferror(infp))
    {
        printf("error can not read file ");
        fclose(infp);
        fclose(outfp);
        return 9;
    }
    fclose(infp);

    if (type==0) 
    {
       if ( PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size, outfp, mode) !=0)
       {
            type=1;
       }
       else
       {
            if (mode > 0)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
            return 0;
       }

       /* fixme */
       return -1;
    }

    if (type== 1)
    {
        if (stricmp(cpuid,"m68000"))
        {
            ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68000,outfp);
            if (mode > 1)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
        }
        else if (stricmp(cpuid,"m68010"))
        {
            ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68010,outfp);
            if (mode > 1)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
            return ret;
        }
        else if (stricmp(cpuid,"m68020"))
        {
            ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68020,outfp);
            if (mode > 1)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
            return ret;
        }
        else if (stricmp(cpuid,"m68030"))
        {
            ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68030,outfp);
            if (mode > 1)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
            return ret;
        }
        else if (stricmp(cpuid,"m68040"))
        {
            ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68040,outfp);
            if (mode > 1)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
            return ret;
        }
        else if (stricmp(cpuid,"ppc"))
        {
            ret = PPCBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,0,outfp);
            if (mode > 1)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
            return ret;
        }
        else if (stricmp(cpuid,"arm4"))
        {
            ret = ARMBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,4,outfp);
            if (mode > 1)
            {
                Convert(outfp,machine_type,mode);
            }
            fclose(outfp);
            return ret;
        }
    }

    if (type==2) 
    {

       ret = PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size, outfp, mode);
       if (mode > 1)
       {
           Convert(outfp,machine_type,mode);
       }
       fclose(outfp);
       return ret;
    }

    return 0;
}

#define  MAXSECTIONNUMBER 16

CPU_INT PEFileStart( CPU_BYTE *memory, CPU_UNINT pos,
                     CPU_UNINT base,  CPU_UNINT size,
                     FILE *outfp, CPU_INT mode)
{
    PIMAGE_DOS_HEADER DosHeader;
    PIMAGE_NT_HEADERS NtHeader;
    IMAGE_SECTION_HEADER SectionHeader[MAXSECTIONNUMBER] = {NULL};
    PIMAGE_SECTION_HEADER pSectionHeader;
    PIMAGE_EXPORT_DIRECTORY ExportEntry;
    INT NumberOfSections;
    INT NumberOfSectionsCount=0;
    INT i;

    DosHeader = (PIMAGE_DOS_HEADER)memory;
    if ( (DosHeader->e_magic != IMAGE_DOS_SIGNATURE) ||
         (size < 0x3c+2) )
    {
        printf("No MZ file \n");
        return -1;
    }

    NtHeader = (PIMAGE_NT_HEADERS) (((ULONG)memory) + ((ULONG)DosHeader->e_lfanew));
    if (NtHeader->Signature != IMAGE_NT_SIGNATURE)
    {
        printf("No PE header found \n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -