2017-12-13 05:47:10 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-11-18 15:53:24 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
|
|
Copyright 2014 Tom Gundersen
|
|
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
***/
|
|
|
|
|
|
2015-11-23 11:23:25 +01:00
|
|
|
#include <endian.h>
|
2014-11-18 15:53:24 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2015-11-23 11:23:25 +01:00
|
|
|
/* BE */
|
|
|
|
|
|
2014-11-18 15:53:24 -05:00
|
|
|
static inline uint16_t unaligned_read_be16(const void *_u) {
|
2018-02-14 14:11:24 +01:00
|
|
|
const struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
|
2014-11-18 15:53:24 -05:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
return be16toh(u->x);
|
2014-11-18 15:53:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint32_t unaligned_read_be32(const void *_u) {
|
2018-02-14 14:11:24 +01:00
|
|
|
const struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
|
2014-11-18 15:53:24 -05:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
return be32toh(u->x);
|
2014-11-18 15:53:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint64_t unaligned_read_be64(const void *_u) {
|
2018-02-14 14:11:24 +01:00
|
|
|
const struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
|
2014-11-18 15:53:24 -05:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
return be64toh(u->x);
|
2014-11-18 15:53:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void unaligned_write_be16(void *_u, uint16_t a) {
|
2018-02-14 14:11:24 +01:00
|
|
|
struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
|
2014-11-18 15:53:24 -05:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
u->x = be16toh(a);
|
2014-11-18 15:53:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void unaligned_write_be32(void *_u, uint32_t a) {
|
2018-02-14 14:11:24 +01:00
|
|
|
struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
|
2014-11-18 15:53:24 -05:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
u->x = be32toh(a);
|
2014-11-18 15:53:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void unaligned_write_be64(void *_u, uint64_t a) {
|
2018-02-14 14:11:24 +01:00
|
|
|
struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
|
2014-11-18 15:53:24 -05:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
u->x = be64toh(a);
|
2014-11-18 15:53:24 -05:00
|
|
|
}
|
2015-11-23 11:23:25 +01:00
|
|
|
|
|
|
|
|
/* LE */
|
|
|
|
|
|
|
|
|
|
static inline uint16_t unaligned_read_le16(const void *_u) {
|
2018-02-14 14:11:24 +01:00
|
|
|
const struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
|
2015-11-23 11:23:25 +01:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
return le16toh(u->x);
|
2015-11-23 11:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint32_t unaligned_read_le32(const void *_u) {
|
2018-02-14 14:11:24 +01:00
|
|
|
const struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
|
2015-11-23 11:23:25 +01:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
return le32toh(u->x);
|
2015-11-23 11:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint64_t unaligned_read_le64(const void *_u) {
|
2018-02-14 14:11:24 +01:00
|
|
|
const struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
|
2015-11-23 11:23:25 +01:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
return le64toh(u->x);
|
2015-11-23 11:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void unaligned_write_le16(void *_u, uint16_t a) {
|
2018-02-14 14:11:24 +01:00
|
|
|
struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
|
2015-11-23 11:23:25 +01:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
u->x = le16toh(a);
|
2015-11-23 11:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void unaligned_write_le32(void *_u, uint32_t a) {
|
2018-02-14 14:11:24 +01:00
|
|
|
struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
|
2015-11-23 11:23:25 +01:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
u->x = le32toh(a);
|
2015-11-23 11:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void unaligned_write_le64(void *_u, uint64_t a) {
|
2018-02-14 14:11:24 +01:00
|
|
|
struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
|
2015-11-23 11:23:25 +01:00
|
|
|
|
2018-02-14 14:11:24 +01:00
|
|
|
u->x = le64toh(a);
|
2015-11-23 11:23:25 +01:00
|
|
|
}
|
2016-06-29 22:38:50 +02:00
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
|
#define unaligned_read_ne16 unaligned_read_be16
|
|
|
|
|
#define unaligned_read_ne32 unaligned_read_be32
|
|
|
|
|
#define unaligned_read_ne64 unaligned_read_be64
|
|
|
|
|
|
|
|
|
|
#define unaligned_write_ne16 unaligned_write_be16
|
|
|
|
|
#define unaligned_write_ne32 unaligned_write_be32
|
|
|
|
|
#define unaligned_write_ne64 unaligned_write_be64
|
|
|
|
|
#else
|
|
|
|
|
#define unaligned_read_ne16 unaligned_read_le16
|
|
|
|
|
#define unaligned_read_ne32 unaligned_read_le32
|
|
|
|
|
#define unaligned_read_ne64 unaligned_read_le64
|
|
|
|
|
|
|
|
|
|
#define unaligned_write_ne16 unaligned_write_le16
|
|
|
|
|
#define unaligned_write_ne32 unaligned_write_le32
|
|
|
|
|
#define unaligned_write_ne64 unaligned_write_le64
|
|
|
|
|
#endif
|