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