pr0gma pr0gma - 1 year ago 47
C Question

How to dump raw memory into a struct fast?

I'm in an identity-mapped memory context (UEFI on x86_64 platform) and I want to dump some contiguous memory content into a structure. Say my structure has this shape:

typedef structure _mystr {
char char_arr[7];
uint32_t u_d;
uint8_t u_b;
} __attribute__((packed)) mystr;

Supposing I have in
the address to the requested memory's offset 0, what's the best way to copy its content into a
instance? Is there a way to do that without iterating through memory with a loop (which seems super boring)?

EDIT: Nicolas Jean suggested the use of
but unfortunately in an
developing context the use of a C standard library makes little sense. However,
CopyMem(IN VOID * dst, IN CONST VOID * src, IN UINTN len)
which carries out the same task.

Answer Source

Copying the memory content with memcpy is probably what you are looking for.

mystr mystr_instance;
memcpy(&mystr_instance, mem_ptr, sizeof(mystr_instance));