2018-11-11 07:48:25 +08:00
|
|
|
#ifndef BUFFER_UTIL_H
|
|
|
|
#define BUFFER_UTIL_H
|
|
|
|
|
2021-01-09 02:24:51 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2018-11-11 07:48:25 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
2019-03-03 06:52:22 +08:00
|
|
|
buffer_write16be(uint8_t *buf, uint16_t value) {
|
2018-11-11 07:48:25 +08:00
|
|
|
buf[0] = value >> 8;
|
|
|
|
buf[1] = value;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
2019-03-03 06:52:22 +08:00
|
|
|
buffer_write32be(uint8_t *buf, uint32_t value) {
|
2018-11-11 07:48:25 +08:00
|
|
|
buf[0] = value >> 24;
|
|
|
|
buf[1] = value >> 16;
|
|
|
|
buf[2] = value >> 8;
|
|
|
|
buf[3] = value;
|
|
|
|
}
|
|
|
|
|
2019-09-15 22:58:14 +08:00
|
|
|
static inline void
|
|
|
|
buffer_write64be(uint8_t *buf, uint64_t value) {
|
|
|
|
buffer_write32be(buf, value >> 32);
|
|
|
|
buffer_write32be(&buf[4], (uint32_t) value);
|
|
|
|
}
|
|
|
|
|
2019-05-29 23:27:41 +08:00
|
|
|
static inline uint16_t
|
|
|
|
buffer_read16be(const uint8_t *buf) {
|
|
|
|
return (buf[0] << 8) | buf[1];
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static inline uint32_t
|
2019-05-29 23:45:46 +08:00
|
|
|
buffer_read32be(const uint8_t *buf) {
|
2021-01-24 22:22:14 +08:00
|
|
|
return ((uint32_t) buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
|
2018-11-11 07:58:29 +08:00
|
|
|
}
|
|
|
|
|
2019-11-30 12:33:40 +08:00
|
|
|
static inline uint64_t
|
|
|
|
buffer_read64be(const uint8_t *buf) {
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t msb = buffer_read32be(buf);
|
|
|
|
uint32_t lsb = buffer_read32be(&buf[4]);
|
|
|
|
return ((uint64_t) msb << 32) | lsb;
|
2018-11-11 07:58:29 +08:00
|
|
|
}
|
|
|
|
|
2018-11-11 07:48:25 +08:00
|
|
|
#endif
|