1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

shared: add helpers for unaligend BE read/write

This commit is contained in:
Tom Gundersen 2014-11-01 14:04:17 +01:00
parent 5d20fde4a5
commit f089257d7b
4 changed files with 157 additions and 0 deletions

1
.gitignore vendored
View File

@ -199,6 +199,7 @@
/test-libudev
/test-libudev-sym*
/test-list
/test-unaligned
/test-locale-util
/test-log
/test-login

View File

@ -746,6 +746,7 @@ libsystemd_shared_la_SOURCES = \
src/shared/securebits.h \
src/shared/special.h \
src/shared/list.h \
src/shared/unaligned.h \
src/shared/macro.h \
src/shared/def.h \
src/shared/sparse-endian.h \
@ -1347,6 +1348,7 @@ tests += \
test-hashmap \
test-set \
test-list \
test-unaligned \
test-tables \
test-device-nodes \
test-xml \
@ -1625,6 +1627,12 @@ test_xml_LDADD = \
test_list_SOURCES = \
src/test/test-list.c
test_unaligned_LDADD = \
libsystemd-core.la
test_unaligned_SOURCES = \
src/test/test-unaligned.c
test_list_LDADD = \
libsystemd-core.la

56
src/shared/unaligned.h Normal file
View File

@ -0,0 +1,56 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#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/>.
***/
#include <stdint.h>
#include "sparse-endian.h"
static inline uint16_t unaligned_read_be16(const be16_t *u) {
return (((uint16_t) ((uint8_t*) u)[0]) << 8) |
((uint16_t) ((uint8_t*) u)[1]);
}
static inline uint32_t unaligned_read_be32(const be32_t *u) {
return (((uint32_t) unaligned_read_be16((be16_t*) u)) << 16) |
((uint32_t) unaligned_read_be16((be16_t*) u + 1));
}
static inline uint64_t unaligned_read_be64(const be64_t *u) {
return (((uint64_t) unaligned_read_be32((be32_t*) u)) << 32) |
((uint64_t) unaligned_read_be32((be32_t*) u + 1));
}
static inline void unaligned_write_be16(be16_t *u, uint16_t a) {
((uint8_t*) u)[0] = (uint8_t) (a >> 8);
((uint8_t*) u)[1] = (uint8_t) a;
}
static inline void unaligned_write_be32(be32_t *u, uint32_t a) {
unaligned_write_be16((be16_t*) u, (uint16_t) (a >> 16));
unaligned_write_be16((be16_t*) u + 1, (uint16_t) a);
}
static inline void unaligned_write_be64(be64_t *u, uint64_t a) {
unaligned_write_be32((be32_t*) u, (uint32_t) (a >> 32));
unaligned_write_be32((be32_t*) u + 1, (uint32_t) a);
}

92
src/test/test-unaligned.c Normal file
View File

@ -0,0 +1,92 @@
/***
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/>.
***/
#include "unaligned.h"
#include "util.h"
static uint8_t data[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};
int main(int argc, const char *argv[]) {
uint8_t scratch[16];
assert_se(unaligned_read_be16((be16_t*)&data[0]) == 0x0001);
assert_se(unaligned_read_be16((be16_t*)&data[1]) == 0x0102);
assert_se(unaligned_read_be32((be32_t*)&data[0]) == 0x00010203);
assert_se(unaligned_read_be32((be32_t*)&data[1]) == 0x01020304);
assert_se(unaligned_read_be32((be32_t*)&data[2]) == 0x02030405);
assert_se(unaligned_read_be32((be32_t*)&data[3]) == 0x03040506);
assert_se(unaligned_read_be64((be64_t*)&data[0]) == 0x0001020304050607);
assert_se(unaligned_read_be64((be64_t*)&data[1]) == 0x0102030405060708);
assert_se(unaligned_read_be64((be64_t*)&data[2]) == 0x0203040506070809);
assert_se(unaligned_read_be64((be64_t*)&data[3]) == 0x030405060708090a);
assert_se(unaligned_read_be64((be64_t*)&data[4]) == 0x0405060708090a0b);
assert_se(unaligned_read_be64((be64_t*)&data[5]) == 0x05060708090a0b0c);
assert_se(unaligned_read_be64((be64_t*)&data[6]) == 0x060708090a0b0c0d);
assert_se(unaligned_read_be64((be64_t*)&data[7]) == 0x0708090a0b0c0d0e);
zero(scratch);
unaligned_write_be16((uint16_t*)&scratch[0], 0x0001);
assert_se(memcmp(&scratch[0], &data[0], sizeof(uint16_t)) == 0);
zero(scratch);
unaligned_write_be16((uint16_t*)&scratch[1], 0x0102);
assert_se(memcmp(&scratch[1], &data[1], sizeof(uint16_t)) == 0);
zero(scratch);
unaligned_write_be32((be32_t*)&scratch[0], 0x00010203);
assert_se(memcmp(&scratch[0], &data[0], sizeof(uint32_t)) == 0);
zero(scratch);
unaligned_write_be32((be32_t*)&scratch[1], 0x01020304);
assert_se(memcmp(&scratch[1], &data[1], sizeof(uint32_t)) == 0);
zero(scratch);
unaligned_write_be32((be32_t*)&scratch[2], 0x02030405);
assert_se(memcmp(&scratch[2], &data[2], sizeof(uint32_t)) == 0);
zero(scratch);
unaligned_write_be32((be32_t*)&scratch[3], 0x03040506);
assert_se(memcmp(&scratch[3], &data[3], sizeof(uint32_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[0], 0x0001020304050607);
assert_se(memcmp(&scratch[0], &data[0], sizeof(uint64_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[1], 0x0102030405060708);
assert_se(memcmp(&scratch[1], &data[1], sizeof(uint64_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[2], 0x0203040506070809);
assert_se(memcmp(&scratch[2], &data[2], sizeof(uint64_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[3], 0x030405060708090a);
assert_se(memcmp(&scratch[3], &data[3], sizeof(uint64_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[4], 0x0405060708090a0b);
assert_se(memcmp(&scratch[4], &data[4], sizeof(uint64_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[5], 0x05060708090a0b0c);
assert_se(memcmp(&scratch[5], &data[5], sizeof(uint64_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[6], 0x060708090a0b0c0d);
assert_se(memcmp(&scratch[6], &data[6], sizeof(uint64_t)) == 0);
zero(scratch);
unaligned_write_be64((be64_t*)&scratch[7], 0x0708090a0b0c0d0e);
assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0);
}