diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2017-07-28 22:01:04 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2017-07-28 22:01:30 +0200 |
| commit | 0898066430e0f2908565a1b4588e50de2d41a256 (patch) | |
| tree | 14cdd602923c0989856faaeaf33352c24c80f440 /src/data.hh | |
| parent | 6bdda0ebabcd8dc34edfc413de6d0424ccf1f6e6 (diff) | |
Break out Package read/write
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 |
