Good library for parsing binary protocols with C++
<p>I'm having to interface my code with some binary protocol coming from an application I cannot control. So far, I have been writing manual parsing from the data, as in:</p>

<pre><code>char *data = ...
MovementPacket pkt;
pkt.x = data[0] | data[1] &lt;&lt; 8;
pkt.y = data[2] | data[3] &lt;&lt; 8;
</code></pre>

<p>It's obvious that this process is very error-prone and tedious. I remember doing it in Java as in:</p>

<pre><code>pkt.x = stream.readShort();
pkt.y = stream.readShort();
</code></pre>

<p>Is there some kind of library that enables me to do this, taking into account endianness? </p>

<p>I've looked into protobuf and cap'n'proto but while both seem great when I can define both sides of the protocol, I'm not sure they would play well if I had to parse a specific protocol. Is that correct or did I just read the wrong resources? Are there alternatives more suited to the task?</p>

<p>The protocol itself is a simple TCP message-oriented format: The handling from TCP stream into messages is already done, I want something to parse the messages themselves. All the messages start with a two-byte identification field, which tells the message's type, followed by message data. An example, for a movement packet:</p>

<pre><code>0x00 0x01 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x02

[Msg. Id] [int32 X value] [int32 Y value]
</code></pre>

<p>I was looking for some library which could, given a message structure, parse it into a struct, taking into account endianness, without having to write manual parsing code.</p>