Information stored in binary data structures is an area so fundamental that there is hardly any field in the industry that doesn’t relate to the parsing or extracting of binary memory sources. However, my intention in this article are so small and specific, that I really want to believe there’s someone else out there who would ever find this useful :)
Binary structures tend to succeed one another. And as long as each of them contains any indication of one’s size, there’s no problem just scrolling through the chain and reading (or memcpy-ing) them one after the other.
Recently I came across a case in which I had to perform parsing of binary headers. There were potentially ten different headers, each of a different size. Nothing’s special, so far.
Examining the data, I found out that the headers were not really attached; there were “padding bytes” between any two headers. Closer examination showed that the headers were DWORD-Aligned. That is, each header starts on the first byte of a DWORD, and in case it doesn’t end on the last byte of a certain DWORD, few padding bytes are added for completion.
This is a little annoying. Before any copying of data you have to make sure you’re pointing to an actually structure and not to a padding byte. It’s really not a big challenge to calculate the next position of the next structure (given the previous structure ending byte) –
BINARY_HEADER *pHeader = NULL;
//pCurByte points to the last byte of the previous strcture.
pCurByte+= sizeof(DWORD) – (pCurByte % sizeof(DWORD) );
pHeader = pCurByte;
However I came across this really cool code in some MS sources I was looking at. They add an Align() method to the structure which returns a pointer to the next DWORD, but the coolest thing about it, is this function implementation (!).
structure BINARY_HEADER
{
byte mField[2];
byte mAnotherField[10];
const BINARY_HEADER* Align()
{
return( (BINARY_HEADER*) ( ( ( (UINT_PTR) this) + 3) & ~3) );
}
};
All you have to do now is –
pHeader = pCurByte;
pCurByte = pCurByte->Align();
Much nicer! :)