I have this code in C++

//...
    char image[0x800];
//...
    auto dosh = reinterpret_cast<IMAGE_DOS_HEADER*>(&image[0]);
    dosh->e_magic = *(WORD*)"MZ";
    dosh->e_cblp = 0x90;
    dosh->e_cp = 3;
    dosh->e_cparhdr = 4;
    dosh->e_maxalloc = 0xffff;
    dosh->e_sp = 0xb8;
    dosh->e_lfarlc = 0x40;
    dosh->e_lfanew = 0x80;
    BYTE stub[] = { 0xb8, 0x01, 0x4c, 0xcd, 0x21 };
    memcpy(&image[0x40], stub, sizeof(stub));


I maded this in D:

module main;

import std.stdio;
import std.c.windows.windows;

struct _IMAGE_DOS_HEADER
{
        WORD e_magic;
        WORD e_cblp;
        WORD e_cp;
        WORD e_crlc;
        WORD e_cparhdr;
        WORD e_minalloc;
        WORD e_maxalloc;
        WORD e_ss;
        WORD e_sp;
        WORD e_csum;
        WORD e_ip;
        WORD e_cs;
        WORD e_lfarlc;
        WORD e_ovno;
        WORD e_res[4];
        WORD e_oemid;
        WORD e_oeminfo;
        WORD e_res2[10];
        LONG e_lfanew;
}
alias IMAGE_DOS_HEADER = _IMAGE_DOS_HEADER;
alias PIMAGE_DOS_HEADER = _IMAGE_DOS_HEADER*;

char image[0x800];

void main(string[] args)
{
        auto dosh = cast(IMAGE_DOS_HEADER*)&image[0];
        //dosh.e_magic = 0x5A4D;
        dosh.e_magic = cast(WORD*)("MZ");

        auto stub = [0xb8, 0x01, 0x4c, 0xcd, 0x21];
        dmemcpy(&image[0x40], stub, stub.sizeof);
}

void * dmemcpy ( void * destination, const void * source, size_t num ) pure nothrow
{
        (cast(ubyte*)destination)[0 ..
                num][]=(cast(const(ubyte)*)source)[0 .. num];
        return destination;
}

But I got errors in this:
dmemcpy(&image[0x40], stub, stub.sizeof);

and in the cast of "MZ" to Word*
How is the best way to solve this ?

Reply via email to