chiark / gitweb /
Prep v228: Condense elogind source masks (1/5)
[elogind.git] / src / basic / unaligned.h
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
24 #include <endian.h>
25 #include <stdint.h>
26
27 /* BE */
28
29 static inline uint16_t unaligned_read_be16(const void *_u) {
30         const uint8_t *u = _u;
31
32         return (((uint16_t) u[0]) << 8) |
33                 ((uint16_t) u[1]);
34 }
35
36 static 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));
41 }
42
43 static 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));
48 }
49
50 static 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;
55 }
56
57 static 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);
62 }
63
64 static 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);
69 }
70
71 /* LE */
72
73 static 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
80 static 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
87 static 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
94 static 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
101 static 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
108 static 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 }