unaligned.h revision 7f034e980de9192d0c7e163de934f19d2f7d3277
2N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2N/A
2N/A#pragma once
2N/A
2N/A/***
2N/A This file is part of systemd.
2N/A
2N/A Copyright 2014 Tom Gundersen
2N/A
2N/A systemd is free software; you can redistribute it and/or modify it
2N/A under the terms of the GNU Lesser General Public License as published by
2N/A the Free Software Foundation; either version 2.1 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A systemd is distributed in the hope that it will be useful, but
2N/A WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2N/A Lesser General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU Lesser General Public License
2790N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
2790N/A***/
5337N/A
2N/A#include <endian.h>
2N/A#include <stdint.h>
2N/A
2N/A/* BE */
2N/A
2N/Astatic inline uint16_t unaligned_read_be16(const void *_u) {
3817N/A const uint8_t *u = _u;
2N/A
2N/A return (((uint16_t) u[0]) << 8) |
2N/A ((uint16_t) u[1]);
59N/A}
59N/A
2N/Astatic inline uint32_t unaligned_read_be32(const void *_u) {
2N/A const uint8_t *u = _u;
2N/A
2N/A return (((uint32_t) unaligned_read_be16(u)) << 16) |
26N/A ((uint32_t) unaligned_read_be16(u + 2));
26N/A}
2N/A
26N/Astatic inline uint64_t unaligned_read_be64(const void *_u) {
1470N/A const uint8_t *u = _u;
38N/A
1470N/A return (((uint64_t) unaligned_read_be32(u)) << 32) |
1470N/A ((uint64_t) unaligned_read_be32(u + 4));
1470N/A}
181N/A
26N/Astatic inline void unaligned_write_be16(void *_u, uint16_t a) {
4811N/A uint8_t *u = _u;
4811N/A
4811N/A u[0] = (uint8_t) (a >> 8);
3739N/A u[1] = (uint8_t) a;
3739N/A}
3739N/A
3739N/Astatic inline void unaligned_write_be32(void *_u, uint32_t a) {
3739N/A uint8_t *u = _u;
3739N/A
3739N/A unaligned_write_be16(u, (uint16_t) (a >> 16));
3739N/A unaligned_write_be16(u + 2, (uint16_t) a);
3817N/A}
3817N/A
26N/Astatic inline void unaligned_write_be64(void *_u, uint64_t a) {
26N/A uint8_t *u = _u;
26N/A
26N/A unaligned_write_be32(u, (uint32_t) (a >> 32));
26N/A unaligned_write_be32(u + 4, (uint32_t) a);
26N/A}
26N/A
700N/A/* LE */
700N/A
26N/Astatic inline uint16_t unaligned_read_le16(const void *_u) {
26N/A const uint8_t *u = _u;
26N/A
1498N/A return (((uint16_t) u[1]) << 8) |
1498N/A ((uint16_t) u[0]);
26N/A}
1498N/A
151N/Astatic inline uint32_t unaligned_read_le32(const void *_u) {
206N/A const uint8_t *u = _u;
26N/A
26N/A return (((uint32_t) unaligned_read_le16(u + 2)) << 16) |
26N/A ((uint32_t) unaligned_read_le16(u));
26N/A}
26N/A
3998N/Astatic inline uint64_t unaligned_read_le64(const void *_u) {
2818N/A const uint8_t *u = _u;
2830N/A
3127N/A return (((uint64_t) unaligned_read_le32(u + 4)) << 32) |
3998N/A ((uint64_t) unaligned_read_le32(u));
26N/A}
3294N/A
26N/Astatic inline void unaligned_write_le16(void *_u, uint16_t a) {
2N/A uint8_t *u = _u;
4747N/A
4437N/A u[0] = (uint8_t) a;
4437N/A u[1] = (uint8_t) (a >> 8);
4488N/A}
4488N/A
4437N/Astatic inline void unaligned_write_le32(void *_u, uint32_t a) {
4437N/A uint8_t *u = _u;
26N/A
26N/A unaligned_write_le16(u, (uint16_t) a);
26N/A unaligned_write_le16(u + 2, (uint16_t) (a >> 16));
26N/A}
883N/A
26N/Astatic inline void unaligned_write_le64(void *_u, uint64_t a) {
26N/A uint8_t *u = _u;
26N/A
26N/A unaligned_write_le32(u, (uint32_t) a);
1043N/A unaligned_write_le32(u + 4, (uint32_t) (a >> 32));
4953N/A}
4953N/A