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