diff options
Diffstat (limited to 'src/data.hh')
| -rw-r--r-- | src/data.hh | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/data.hh b/src/data.hh new file mode 100644 index 0000000..e949b47 --- /dev/null +++ b/src/data.hh @@ -0,0 +1,35 @@ +// -*- mode: c++; c-basic-offset: 2; -*- + +#ifndef DATA_HH +#define DATA_HH + +#include <cstdint> + +inline uint16_t read_u16(uint8_t const* data) { + return data[0] << 8 | data[1]; +} + +inline uint32_t read_u32(uint8_t const* data) { + return static_cast<uint32_t>(read_u16(data)) << 16 | read_u16(data + 2); +} + +inline uint64_t read_u64(uint8_t const* data) { + return static_cast<uint64_t>(read_u32(data)) << 32 | read_u32(data + 4); +} + +inline void write_u16(uint8_t* dst, uint16_t value) { + dst[0] = value >> 8; + dst[1] = value & 0xff; +} + +inline void write_u32(uint8_t* dst, uint32_t value) { + write_u16(dst, value >> 16); + write_u16(dst + 2, value & 0xffff); +} + +inline void write_u64(uint8_t* dst, uint64_t value) { + write_u32(dst, value >> 32); + write_u32(dst + 4, value & 0xffffffff); +} + +#endif // DATA_HH |
