]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/unaligned.h
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / basic / unaligned.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f089257d
TG
2#pragma once
3
7f034e98 4#include <endian.h>
f089257d
TG
5#include <stdint.h>
6
7f034e98
DM
7/* BE */
8
617e7946 9static inline uint16_t unaligned_read_be16(const void *_u) {
012c2f76 10 const struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
f089257d 11
b61e5f17 12 return be16toh(u->x);
f089257d
TG
13}
14
617e7946 15static inline uint32_t unaligned_read_be32(const void *_u) {
012c2f76 16 const struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
617e7946 17
b61e5f17 18 return be32toh(u->x);
f089257d
TG
19}
20
617e7946 21static inline uint64_t unaligned_read_be64(const void *_u) {
012c2f76 22 const struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
617e7946 23
b61e5f17 24 return be64toh(u->x);
f089257d
TG
25}
26
617e7946 27static inline void unaligned_write_be16(void *_u, uint16_t a) {
012c2f76 28 struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
617e7946 29
b61e5f17 30 u->x = be16toh(a);
f089257d
TG
31}
32
617e7946 33static inline void unaligned_write_be32(void *_u, uint32_t a) {
012c2f76 34 struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
617e7946 35
b61e5f17 36 u->x = be32toh(a);
f089257d
TG
37}
38
617e7946 39static inline void unaligned_write_be64(void *_u, uint64_t a) {
012c2f76 40 struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
617e7946 41
b61e5f17 42 u->x = be64toh(a);
f089257d 43}
7f034e98
DM
44
45/* LE */
46
47static inline uint16_t unaligned_read_le16(const void *_u) {
012c2f76 48 const struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
7f034e98 49
b61e5f17 50 return le16toh(u->x);
7f034e98
DM
51}
52
53static inline uint32_t unaligned_read_le32(const void *_u) {
012c2f76 54 const struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
7f034e98 55
b61e5f17 56 return le32toh(u->x);
7f034e98
DM
57}
58
59static inline uint64_t unaligned_read_le64(const void *_u) {
012c2f76 60 const struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
7f034e98 61
b61e5f17 62 return le64toh(u->x);
7f034e98
DM
63}
64
65static inline void unaligned_write_le16(void *_u, uint16_t a) {
012c2f76 66 struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
7f034e98 67
b61e5f17 68 u->x = le16toh(a);
7f034e98
DM
69}
70
71static inline void unaligned_write_le32(void *_u, uint32_t a) {
012c2f76 72 struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
7f034e98 73
b61e5f17 74 u->x = le32toh(a);
7f034e98
DM
75}
76
77static inline void unaligned_write_le64(void *_u, uint64_t a) {
012c2f76 78 struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
7f034e98 79
b61e5f17 80 u->x = le64toh(a);
7f034e98 81}
c917a321
LP
82
83#if __BYTE_ORDER == __BIG_ENDIAN
84#define unaligned_read_ne16 unaligned_read_be16
85#define unaligned_read_ne32 unaligned_read_be32
86#define unaligned_read_ne64 unaligned_read_be64
87
88#define unaligned_write_ne16 unaligned_write_be16
89#define unaligned_write_ne32 unaligned_write_be32
90#define unaligned_write_ne64 unaligned_write_be64
91#else
92#define unaligned_read_ne16 unaligned_read_le16
93#define unaligned_read_ne32 unaligned_read_le32
94#define unaligned_read_ne64 unaligned_read_le64
95
96#define unaligned_write_ne16 unaligned_write_le16
97#define unaligned_write_ne32 unaligned_write_le32
98#define unaligned_write_ne64 unaligned_write_le64
99#endif