C++ – binary protocol – byte swap trick

c++

lets say we have a binary protocol, with fields network ordered (big endian).

struct msg1
{
    int32 a;
    int16 b;
    uint32 c
}

if instead of copying the network buffer to my msg1 and then use the "networkToHost" functions to read msg1

I rearrange / reverse msg1 to

struct msg1
{
    uint32 c
    int16 b;
    int32 a;
}

and simply do a reverse copy from the network buffer to create msg1. In that case, there is no need for networkToHost functions. this idiomatic approach doesn't work in big endian machines but for me this is not a problem. Apart from that, is there any other drawback that I miss?

thanks

P.S. for the above we enforce strict alignment(#pragma pack(1) etc)

Best Solution

Apart from that, is there any other drawback that I miss?

I'm afraid you've misunderstood the nature of endian conversion problems. "Big endian" doesn't mean your fields are laid out in reverse, so that a

struct msg1_bigendian
{
    int32 a;
    int16 b;
    uint32 c
}

on a big endian architecture is equivalent to a

struct msg1_littleendian 
{
   uint32 c;
   int16 b;
   int32 a;
}

on a little endian architecture. Rather, it means that the byte-order within each field is reversed. Let's assume:

a = 0x1000000a;
b = 0xb;
c = 0xc;

On a big-endian architecture, this will be laid out as:

10 00 00 0a
00 0b
00 00 00 0c

The high-order (most significant) byte comes first.

On a little-endian machine, this will be laid out as:

0a 00 00 10
0b 00
0c 00 00 00

The lowest order byte comes first, the highest order last.

Serialize them and overlay the serialized form of the messages on top of each other, and you will discover the incompatibility:

10 00 00 0a  00 0b  00 00 00 0c (big endian)
0a 00 00 10  0b 00  0c 00 00 00 (little endian)

   int32 a  int16 b    int32 c

Note that this isn't simply a case of the fields running in reverse. You proposal would result in a little endian machine mistaking the big endian representation as:

a = 0xc000000; b = 0xb00; c = 0xa000010;

Certainly not what was transmitted!

You really do have to convert every individual field to network byte order and back again, for every field transmitted.

UPDATE:

Ok, I understand what you are trying to do now. You want to define the struct in reverse, then memcpy from the end of the byte string to the beginning (reverse copy) and reverse the byte order that way. In which case I would say, yes, this is a hack, and yes, it makes your code un-portable, and yes, it isn't worth it. Converting between byte orders is not, in fact, a very expensive operation and it is far easier to deal with than reversing the layout of every structure.