]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/unaligned.h
dhcp6-option: Add helper function for uncompressed domain names
[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
24#include <stdint.h>
25
617e7946
TG
26static inline uint16_t unaligned_read_be16(const void *_u) {
27 const uint8_t *u = _u;
f089257d 28
617e7946
TG
29 return (((uint16_t) u[0]) << 8) |
30 ((uint16_t) u[1]);
f089257d
TG
31}
32
617e7946
TG
33static inline uint32_t unaligned_read_be32(const void *_u) {
34 const uint8_t *u = _u;
35
36 return (((uint32_t) unaligned_read_be16(u)) << 16) |
37 ((uint32_t) unaligned_read_be16(u + 2));
f089257d
TG
38}
39
617e7946
TG
40static inline uint64_t unaligned_read_be64(const void *_u) {
41 const uint8_t *u = _u;
42
43 return (((uint64_t) unaligned_read_be32(u)) << 32) |
44 ((uint64_t) unaligned_read_be32(u + 4));
f089257d
TG
45}
46
617e7946
TG
47static inline void unaligned_write_be16(void *_u, uint16_t a) {
48 uint8_t *u = _u;
49
50 u[0] = (uint8_t) (a >> 8);
51 u[1] = (uint8_t) a;
f089257d
TG
52}
53
617e7946
TG
54static inline void unaligned_write_be32(void *_u, uint32_t a) {
55 uint8_t *u = _u;
56
57 unaligned_write_be16(u, (uint16_t) (a >> 16));
58 unaligned_write_be16(u + 2, (uint16_t) a);
f089257d
TG
59}
60
617e7946
TG
61static inline void unaligned_write_be64(void *_u, uint64_t a) {
62 uint8_t *u = _u;
63
64 unaligned_write_be32(u, (uint32_t) (a >> 32));
65 unaligned_write_be32(u + 4, (uint32_t) a);
f089257d 66}