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