]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/unaligned.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / unaligned.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f089257d
TG
2#pragma once
3
4/***
5 This file is part of systemd.
6
7 Copyright 2014 Tom Gundersen
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
7f034e98 23#include <endian.h>
f089257d
TG
24#include <stdint.h>
25
7f034e98
DM
26/* BE */
27
617e7946
TG
28static inline uint16_t unaligned_read_be16(const void *_u) {
29 const uint8_t *u = _u;
f089257d 30
617e7946
TG
31 return (((uint16_t) u[0]) << 8) |
32 ((uint16_t) u[1]);
f089257d
TG
33}
34
617e7946
TG
35static inline uint32_t unaligned_read_be32(const void *_u) {
36 const uint8_t *u = _u;
37
38 return (((uint32_t) unaligned_read_be16(u)) << 16) |
39 ((uint32_t) unaligned_read_be16(u + 2));
f089257d
TG
40}
41
617e7946
TG
42static inline uint64_t unaligned_read_be64(const void *_u) {
43 const uint8_t *u = _u;
44
45 return (((uint64_t) unaligned_read_be32(u)) << 32) |
46 ((uint64_t) unaligned_read_be32(u + 4));
f089257d
TG
47}
48
617e7946
TG
49static inline void unaligned_write_be16(void *_u, uint16_t a) {
50 uint8_t *u = _u;
51
52 u[0] = (uint8_t) (a >> 8);
53 u[1] = (uint8_t) a;
f089257d
TG
54}
55
617e7946
TG
56static inline void unaligned_write_be32(void *_u, uint32_t a) {
57 uint8_t *u = _u;
58
59 unaligned_write_be16(u, (uint16_t) (a >> 16));
60 unaligned_write_be16(u + 2, (uint16_t) a);
f089257d
TG
61}
62
617e7946
TG
63static inline void unaligned_write_be64(void *_u, uint64_t a) {
64 uint8_t *u = _u;
65
66 unaligned_write_be32(u, (uint32_t) (a >> 32));
67 unaligned_write_be32(u + 4, (uint32_t) a);
f089257d 68}
7f034e98
DM
69
70/* LE */
71
72static inline uint16_t unaligned_read_le16(const void *_u) {
73 const uint8_t *u = _u;
74
75 return (((uint16_t) u[1]) << 8) |
76 ((uint16_t) u[0]);
77}
78
79static inline uint32_t unaligned_read_le32(const void *_u) {
80 const uint8_t *u = _u;
81
82 return (((uint32_t) unaligned_read_le16(u + 2)) << 16) |
83 ((uint32_t) unaligned_read_le16(u));
84}
85
86static inline uint64_t unaligned_read_le64(const void *_u) {
87 const uint8_t *u = _u;
88
89 return (((uint64_t) unaligned_read_le32(u + 4)) << 32) |
90 ((uint64_t) unaligned_read_le32(u));
91}
92
93static inline void unaligned_write_le16(void *_u, uint16_t a) {
94 uint8_t *u = _u;
95
96 u[0] = (uint8_t) a;
97 u[1] = (uint8_t) (a >> 8);
98}
99
100static inline void unaligned_write_le32(void *_u, uint32_t a) {
101 uint8_t *u = _u;
102
103 unaligned_write_le16(u, (uint16_t) a);
104 unaligned_write_le16(u + 2, (uint16_t) (a >> 16));
105}
106
107static inline void unaligned_write_le64(void *_u, uint64_t a) {
108 uint8_t *u = _u;
109
110 unaligned_write_le32(u, (uint32_t) a);
111 unaligned_write_le32(u + 4, (uint32_t) (a >> 32));
112}
c917a321
LP
113
114#if __BYTE_ORDER == __BIG_ENDIAN
115#define unaligned_read_ne16 unaligned_read_be16
116#define unaligned_read_ne32 unaligned_read_be32
117#define unaligned_read_ne64 unaligned_read_be64
118
119#define unaligned_write_ne16 unaligned_write_be16
120#define unaligned_write_ne32 unaligned_write_be32
121#define unaligned_write_ne64 unaligned_write_be64
122#else
123#define unaligned_read_ne16 unaligned_read_le16
124#define unaligned_read_ne32 unaligned_read_le32
125#define unaligned_read_ne64 unaligned_read_le64
126
127#define unaligned_write_ne16 unaligned_write_le16
128#define unaligned_write_ne32 unaligned_write_le32
129#define unaligned_write_ne64 unaligned_write_le64
130#endif