summaryrefslogtreecommitdiff
path: root/src/data.hh
blob: e949b475dc6dbdc29ecc674cb26e996a2316ca5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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