diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 8 | ||||
-rw-r--r-- | src/shared/unaligned.h | 56 | ||||
-rw-r--r-- | src/test/test-unaligned.c | 92 |
4 files changed, 157 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore index 23522eddd2..2e2c64e28a 100644 --- a/.gitignore +++ b/.gitignore @@ -199,6 +199,7 @@ /test-libudev /test-libudev-sym* /test-list +/test-unaligned /test-locale-util /test-log /test-login diff --git a/Makefile.am b/Makefile.am index 3f1ace0d6c..ea4da0b3d1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/src/shared/unaligned.h b/src/shared/unaligned.h new file mode 100644 index 0000000000..b194d71017 --- /dev/null +++ b/src/shared/unaligned.h @@ -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); +} diff --git a/src/test/test-unaligned.c b/src/test/test-unaligned.c new file mode 100644 index 0000000000..52693cd325 --- /dev/null +++ b/src/test/test-unaligned.c @@ -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); +} |