]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/unaligned.h
hexdecoct: make unbase64mem and unhexmem always use SIZE_MAX
[thirdparty/systemd.git] / src / basic / unaligned.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f089257d
TG
2#pragma once
3
7f034e98 4#include <endian.h>
f089257d
TG
5#include <stdint.h>
6
d8007e7a
LP
7#include "unaligned-fundamental.h"
8
7f034e98
DM
9/* BE */
10
617e7946 11static inline uint16_t unaligned_read_be16(const void *_u) {
012c2f76 12 const struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
f089257d 13
b61e5f17 14 return be16toh(u->x);
f089257d
TG
15}
16
617e7946 17static inline uint32_t unaligned_read_be32(const void *_u) {
012c2f76 18 const struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
617e7946 19
b61e5f17 20 return be32toh(u->x);
f089257d
TG
21}
22
617e7946 23static inline uint64_t unaligned_read_be64(const void *_u) {
012c2f76 24 const struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
617e7946 25
b61e5f17 26 return be64toh(u->x);
f089257d
TG
27}
28
617e7946 29static inline void unaligned_write_be16(void *_u, uint16_t a) {
012c2f76 30 struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
617e7946 31
b61e5f17 32 u->x = be16toh(a);
f089257d
TG
33}
34
617e7946 35static inline void unaligned_write_be32(void *_u, uint32_t a) {
012c2f76 36 struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
617e7946 37
b61e5f17 38 u->x = be32toh(a);
f089257d
TG
39}
40
617e7946 41static inline void unaligned_write_be64(void *_u, uint64_t a) {
012c2f76 42 struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
617e7946 43
b61e5f17 44 u->x = be64toh(a);
f089257d 45}
7f034e98
DM
46
47/* LE */
48
49static inline uint16_t unaligned_read_le16(const void *_u) {
012c2f76 50 const struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
7f034e98 51
b61e5f17 52 return le16toh(u->x);
7f034e98
DM
53}
54
55static inline uint32_t unaligned_read_le32(const void *_u) {
012c2f76 56 const struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
7f034e98 57
b61e5f17 58 return le32toh(u->x);
7f034e98
DM
59}
60
61static inline uint64_t unaligned_read_le64(const void *_u) {
012c2f76 62 const struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
7f034e98 63
b61e5f17 64 return le64toh(u->x);
7f034e98
DM
65}
66
67static inline void unaligned_write_le16(void *_u, uint16_t a) {
012c2f76 68 struct __attribute__((__packed__, __may_alias__)) { uint16_t x; } *u = _u;
7f034e98 69
b61e5f17 70 u->x = le16toh(a);
7f034e98
DM
71}
72
73static inline void unaligned_write_le32(void *_u, uint32_t a) {
012c2f76 74 struct __attribute__((__packed__, __may_alias__)) { uint32_t x; } *u = _u;
7f034e98 75
b61e5f17 76 u->x = le32toh(a);
7f034e98
DM
77}
78
79static inline void unaligned_write_le64(void *_u, uint64_t a) {
012c2f76 80 struct __attribute__((__packed__, __may_alias__)) { uint64_t x; } *u = _u;
7f034e98 81
b61e5f17 82 u->x = le64toh(a);
7f034e98 83}