Results 1 to 6 of 6

Thread: Map a file directly into memory using C

  1. #1
    Join Date
    Feb 2014
    Beans
    8

    Map a file directly into memory using C

    Hello guys,
    I'm trying to study more about PE headers, so i wanted to load a file diretly into memory and then print his PE headers.
    To do this i first use fopen() and fread()
    Code:
    FILE* fd = fopen("test.exe","r");
    fread(buffer, sizeof(short),1,fd);
    printf("The magic is: %s",buffer);
    And then o got the "MZ".
    Now i'm trying to simulate a file structure:
    Code:
    STRUCT dos_header{
      char e_magic[1];
    ...
    }
    But here is here i got stuck. In order for me to "point" this structure on the exe, i have to be able to load this exe directly onto memory (without using a file descriptor i think). Do you guys know a way to do this?
    Just to better explain what im thinking, i wanted to do the following:

    Code:
    //Magicaly load the exe onto memory and a pointer named "p_EXE" points to the first byte, so:
    dos_header* dheader = p_EXE;
    printf("The magic is %s", dheader->e_magic);
    Can you guys help me!?
    Thanks all!

  2. #2
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Map a file directly into memory using C

    You might want to look into opening the file with "rb" (binary) mode, where what would ordinairily be new line sequences can have a different meaning than in "r" (text) mode.
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.

  3. #3
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Map a file directly into memory using C

    If you are running your program on Windows, then you probably need rb indeed. In Linux (or other POSIX-compliant systems), it has no effet (r and rb are equivalent).
    「明後日の夕方には帰ってるからね。」


  4. #4
    Join Date
    Feb 2014
    Beans
    8

    Re: Map a file directly into memory using C

    yep. I think i need to, somehow, load the entire file on the memory and the access it without using a FILE structure...am i right?

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,912
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Map a file directly into memory using C

    You can use mmap() and derivatives to map a range of addresses to the file (never tried that though).

    Otherwise you can use your FILE to read the whole file in memory and if you have a pointer to the first byte, and a struct describing the header, you can cast the pointer to a pointer to that struct and read the header elements.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  6. #6
    Join Date
    Feb 2014
    Beans
    8

    Re: Map a file directly into memory using C

    Thank you!
    Could you please show me some code, because i still don't know how to implement that

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •