This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Harald Welte gerrit-no-reply at lists.osmocom.orgHarald Welte has submitted this change and it was merged. Change subject: introduce byteswap.h with osmo_{htonl,ntohl,htons,ntohs} ...................................................................... introduce byteswap.h with osmo_{htonl,ntohl,htons,ntohs} We need to have an architecture-independend way of endian conversion / byte swapping functions which will also work on embedded (bare iron) builds. Let's introduce osmocom/core/bytesawp.h for this purpose. Change-Id: Ibc0cc1e36d4ed63a35cf8ceff3af0f26e5ac7a3d --- M include/Makefile.am A include/osmocom/core/byteswap.h M tests/Makefile.am A tests/endian/endian_test.c A tests/endian/endian_test.ok M tests/testsuite.at 6 files changed, 89 insertions(+), 2 deletions(-) Approvals: Harald Welte: Looks good to me, approved Jenkins Builder: Verified diff --git a/include/Makefile.am b/include/Makefile.am index 0383d7a..6981c37 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -10,6 +10,7 @@ osmocom/core/bits.h \ osmocom/core/bitvec.h \ osmocom/core/bitcomp.h \ + osmocom/core/byteswap.h \ osmocom/core/conv.h \ osmocom/core/crc16.h \ osmocom/core/crc16gen.h \ diff --git a/include/osmocom/core/byteswap.h b/include/osmocom/core/byteswap.h new file mode 100644 index 0000000..1f09c2b --- /dev/null +++ b/include/osmocom/core/byteswap.h @@ -0,0 +1,43 @@ +#pragma once +#include <stdint.h> +#include <osmocom/core/endian.h> + +/*! \brief byte-swap a 32bit word + * \param[in] in to be swapped 32bit word + * \returns byte-swapped 32bit word */ +static inline uint32_t osmo_swab32(uint32_t in) +{ + uint32_t out; + + out = (in & 0xff) << 24; + out |= (in & 0xff00) << 8; + out |= (in & 0xff0000) >> 8; + out |= (in & 0xff000000) >> 24; + + return out; +} + +/*! \brief byte-swap a 16bit word + * \param[in] in to be swapped 16bit word + * \returns byte-swapped 16bit word */ +static inline uint16_t osmo_swab16(uint16_t in) +{ + uint16_t out; + + out = (in & 0xff) << 8; + out |= (in & 0xff00) >> 8; + + return out; +} + +#ifdef OSMO_IS_LITTLE_ENDIAN +#define osmo_ntohl(x) osmo_swab32(x) +#define osmo_ntohs(x) osmo_swab16(x) +#define osmo_htonl(x) osmo_swab32(x) +#define osmo_htons(x) osmo_swab16(x) +#else +#define osmo_ntohl(x) (x) +#define osmo_ntohs(x) (x) +#define osmo_htonl(x) (x) +#define osmo_htons(x) (x) +#endif diff --git a/tests/Makefile.am b/tests/Makefile.am index d9816ef..d95083b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -16,7 +16,7 @@ tlv/tlv_test gsup/gsup_test oap/oap_test fsm/fsm_test \ write_queue/wqueue_test socket/socket_test \ coding/coding_test conv/conv_gsm0503_test \ - abis/abis_test + abis/abis_test endian/endian_test if ENABLE_MSGFILE check_PROGRAMS += msgfile/msgfile_test @@ -162,6 +162,9 @@ $(top_builddir)/src/codec/libosmocodec.la \ $(top_builddir)/src/coding/libosmocoding.la +endian_endian_test_SOURCES = endian/endian_test.c +endian_endian_test_LDADD = $(top_builddir)/src/libosmocore.la + # The `:;' works around a Bash 3.2 bug when the output is not writeable. $(srcdir)/package.m4: $(top_srcdir)/configure.ac :;{ \ @@ -204,7 +207,7 @@ osmo-auc-gen/osmo-auc-gen_test.sh \ osmo-auc-gen/osmo-auc-gen_test.ok \ osmo-auc-gen/osmo-auc-gen_test.err \ - conv/conv_gsm0503_test.ok + conv/conv_gsm0503_test.ok endian/endian_test.ok DISTCLEANFILES = atconfig atlocal conv/gsm0503_test_vectors.c BUILT_SOURCES = conv/gsm0503_test_vectors.c diff --git a/tests/endian/endian_test.c b/tests/endian/endian_test.c new file mode 100644 index 0000000..8737e7a --- /dev/null +++ b/tests/endian/endian_test.c @@ -0,0 +1,31 @@ +#include <osmocom/core/byteswap.h> +#include <osmocom/core/utils.h> + +int main(int argc, char **argv) +{ + printf("Testing 16bit swappinng\n"); + OSMO_ASSERT(osmo_swab16(0x1234) == 0x3412); + printf("Testing 32bit swappinng\n"); + OSMO_ASSERT(osmo_swab32(0x12345678) == 0x78563412); + + printf("Testing ntohX() and htonX()\n"); +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#if OSMO_IS_LITTLE_ENDIAN == 0 +#error "Something wrong with endianness detection!" +#endif /* IS_LITTLE_ENDIAN */ + OSMO_ASSERT(osmo_ntohs(0x1234) == 0x3412); + OSMO_ASSERT(osmo_htons(0x1234) == 0x3412); + OSMO_ASSERT(osmo_htonl(0x12345678) == 0x78563412); + OSMO_ASSERT(osmo_ntohl(0x12345678) == 0x78563412); +#else +#if OSMO_IS_LITTLE_ENDIAN == 1 +#error "Something wrong with endianness detection!" +#endif /* IS_LITTLE_ENDIAN */ + OSMO_ASSERT(osmo_ntohs(0x1234) == 0x1234); + OSMO_ASSERT(osmo_htons(0x1234) == 0x1234); + OSMO_ASSERT(osmo_htonl(0x12345678) == 0x12345678); + OSMO_ASSERT(osmo_ntohl(0x12345678) == 0x12345678); +#endif /* __BYTE_ORDER__ */ + + exit(0); +} diff --git a/tests/endian/endian_test.ok b/tests/endian/endian_test.ok new file mode 100644 index 0000000..a42f457 --- /dev/null +++ b/tests/endian/endian_test.ok @@ -0,0 +1,3 @@ +Testing 16bit swappinng +Testing 32bit swappinng +Testing ntohX() and htonX() diff --git a/tests/testsuite.at b/tests/testsuite.at index 2f9cfe7..4c8950c 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -268,3 +268,9 @@ cat $abs_srcdir/osmo-auc-gen/osmo-auc-gen_test.err > experr AT_CHECK([$abs_top_srcdir/tests/osmo-auc-gen/osmo-auc-gen_test.sh $abs_top_builddir/utils/osmo-auc-gen], [0], [expout], [experr]) AT_CLEANUP + +AT_SETUP([endian]) +AT_KEYWORDS([endian]) +cat $abs_srcdir/endian/endian_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/endian/endian_test], [0], [expout], [ignore]) +AT_CLEANUP -- To view, visit https://gerrit.osmocom.org/2623 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ibc0cc1e36d4ed63a35cf8ceff3af0f26e5ac7a3d Gerrit-PatchSet: 2 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Harald Welte <laforge at gnumonks.org> Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org> Gerrit-Reviewer: Jenkins Builder