1

I'm reading values from a (PE) binary file. The values have a known length and position in the file and are stored with no padding (right next to each other). They store various types (int, long, pointer, array...).

I'm trying to parse those binary structures and store them in a useful way. The obvious way to do it is to just:

struct IMAGE_FILE_HEADER {
    WORD Machine;
    WORD NumberOfSections;
    DWORD TimeDateStamp;
    DWORD PointerToSymbolTable;
    DWORD NumberOfSymbols;
    WORD SizeOfOptionalHeader;
    WORD Characteristics;
};

However, this has the downside that, in writing the values to the struct, I have to manually write (using the assignment operator) every value one by one, which is time consuming and requires a bunch of boilerplate code (I have hundreds of these values). The upside is that it's type-safe. Doing a memcpy is not feasible because of struct memory alignment issues.

Another option is to use an unordered map to store the key-value pairs, but this doesn't work because my binary data has values of various types. I could create a map for every type, but that just seems unnecessarily complicated.

Polymorphism is a third option. I could write a BaseValue class and have each of the types inherit from BaseValue. Then I could just store a map of BaseValue types. This would work, but it's extremely hacky and not type-safe.

It seems like such a simple task. Just read a binary file with different types into a structure. How can I do this task in a way that doesn't require a bunch of boilerplate code and is type-safe? Thank you.

Edit:

The boilerplate would just consist of hundreds of lines like this one:

this.NumberOfSections = data.Read<WORD>(1234);
10
  • 1
    Could you show a snippet of your boilerplate? I have a feeling that function overloading could lead to a terse description of parsing, similar to standard IO streams: IMAGE_FILE_HEADER h {}; parser >> h.Machine >> h.NumberOfSections >> h.TimeDateStamp >> ... where template<class T> Parser& operator>>(Parser&, T& target)
    – amon
    May 23 at 9:54
  • 2
    Struct alignment issues can be controlled, see stackoverflow.com/questions/21092415/…
    – Doc Brown
    May 23 at 9:58
  • Since you have been here in between and did not react to my former comment, I guess I did not make myself clear enough: it would be nice if you could edit your question and tell us why the issue cannot be solved by utilizing structure alignment pragmas.
    – Doc Brown
    May 23 at 11:01
  • @DocBrown Accessing unaligned data in C++ is undefined behavior because it's not specified by the standard. Therefore it's not guaranteed to work on all platforms, and I need to to work with Windows XP for a specific reason. In theory it is a possible solution.
    – Ciprum
    May 23 at 11:09
  • 1
    @DocBrown After more testing, it does appear to work. I thought it didn't because I checked the magic which is not big-endian in a PE file, unlike the rest of the values.
    – Ciprum
    May 23 at 12:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.