/src/ntpsec/include/ntp_endian.h
Line | Count | Source |
1 | | /* ntp_endian.h - BSD-style big-endian encoding/decoding functions |
2 | | * |
3 | | * Copyright the NTPsec project contributors |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | * |
6 | | * These are portable reimplementations of what BSD provides |
7 | | * in <sys/endian.h>, but using uint8_t instead of void *. |
8 | | */ |
9 | | #ifndef GUARD_NTP_ENDIAN_H |
10 | | #define GUARD_NTP_ENDIAN_H |
11 | | |
12 | | #include <stdint.h> |
13 | | |
14 | 978 | static inline uint16_t ntp_be16dec(const uint8_t *buf) { |
15 | 978 | return (uint16_t)(((uint16_t)buf[0] << 8) | buf[1]); |
16 | 978 | } |
17 | | |
18 | 0 | static inline uint32_t ntp_be32dec(const uint8_t *buf) { |
19 | 0 | return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) | |
20 | 0 | ((uint32_t)buf[2] << 8) | buf[3]; |
21 | 0 | } |
22 | | |
23 | 0 | static inline uint64_t ntp_be64dec(const uint8_t *buf) { |
24 | 0 | return ((uint64_t)ntp_be32dec(buf) << 32) | ntp_be32dec(buf + 4); |
25 | 0 | } |
26 | | |
27 | 0 | static inline void ntp_be16enc(uint8_t *buf, uint16_t v) { |
28 | 0 | buf[0] = (uint8_t)(v >> 8); |
29 | 0 | buf[1] = (uint8_t)v; |
30 | 0 | } |
31 | | |
32 | | #endif |