Skip to content

Instantly share code, notes, and snippets.

@kblomqvist
Last active December 24, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kblomqvist/83d21996c28b9fec8756d78128fc13a5 to your computer and use it in GitHub Desktop.
Save kblomqvist/83d21996c28b9fec8756d78128fc13a5 to your computer and use it in GitHub Desktop.
Deserialize bytes
#undef be16toh
void deserialize_u32(uint32_t &value, const uint8_t *bytes)
{
value =
(static_cast<uint32_t>(bytes[0]) << 24) |
(bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}
void deserialize_u24(uint32_t &value, const uint8_t *bytes)
{
value =
(static_cast<uint32_t>(bytes[0]) << 16) |
(bytes[1] << 8) | bytes[2];
}
uint16_t be16toh(const uint8_t *bytes)
{
return = (static_cast<uint16_t>(bytes[0]) << 8) | bytes[1];
}
// Returns number of loose bytes
int deserialize_u32(uint32_t *dest, const uint8_t *bytes, int n)
{
while (n > 3) {
deserialize_u32(*dest++, bytes);
bytes += 4;
n -= 4;
}
return n;
}
// Returns number of loose bytes
int deserialize_u24(uint32_t *dest, const uint8_t *bytes, int n)
{
while (n > 2) {
deserialize_u24(*dest++, bytes);
bytes += 3;
n -= 3;
}
return n;
}
// Returns number of encoded values
int be16toh(uint16_t *dest, const uint8_t *bytes, int n)
{
int i = 0;
for (; n > 1; n-=2, i++) {
dest[i] = be16toh(bytes);
bytes += 2;
}
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment