[PATCH] libosmocore[master]: introduce byteswap.h with osmo_{htonl, ntohl, htons, ntohs}

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.org
Mon May 15 11:14:18 UTC 2017


Review at  https://gerrit.osmocom.org/2623

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
2 files changed, 38 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/23/2623/1

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..55db8ed
--- /dev/null
+++ b/include/osmocom/core/byteswap.h
@@ -0,0 +1,37 @@
+#pragma once
+#include <stdint.h>
+#include <osmocom/core/endian.h>
+
+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;
+}
+
+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)
+#elif
+#define osmo_ntohl(x)	(x)
+#define osmo_ntohs(x)	(x)
+#define osmo_htonl(x)	(x)
+#define osmo_htons(x)	(x)
+#endif

-- 
To view, visit https://gerrit.osmocom.org/2623
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibc0cc1e36d4ed63a35cf8ceff3af0f26e5ac7a3d
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>



More information about the gerrit-log mailing list