Upcoming commits will be implementing the ChaCha20 high-speed
symmetric hashing algorithm as part of a CSPRNG for use with
NAT tuple generation.  ChaCha20 treats internal stream data
as little endian bytes, which means the implementation will
need a way to read and write byte streams of le32 values.

To facilitate this, try to follow the proposed POSIX endian
header support as much as possible.  Linux and FreeBSD
differ slightly in how they provide the htole32 and le32toh
function defintions, but they both provide implementations.

Signed-off-by: Aaron Conole <[email protected]>
---
 lib/byte-order.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/lib/byte-order.h b/lib/byte-order.h
index f5a21f3298..c942e78a96 100644
--- a/lib/byte-order.h
+++ b/lib/byte-order.h
@@ -20,6 +20,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <inttypes.h>
+#include <string.h>
 #include "openvswitch/types.h"
 
 #ifndef __CHECKER__
@@ -71,6 +72,39 @@ uint32_byteswap(uint32_t crc) {
             ((crc & 0xff000000) >> 24));
 }
 
+#ifdef __linux__
+#include <endian.h>
+#elif defined(__FreeBSD__)
+#include <sys/endian.h>
+#else
+static inline uint32_t
+htole32(uint32_t host_32bits)
+{
+    return htonl(1) == 1 ? uint32_byteswap(host_32bits) : host_32bits;
+}
+
+static inline uint32_t
+le32toh(uint32_t le_32bits)
+{
+    return htonl(1) == 1 ? uint32_byteswap(le_32bits) : le_32bits;
+}
+#endif
+
+static inline uint32_t
+ovs_load_le32(const uint8_t *p)
+{
+    uint32_t n;
+    memcpy(&n, p, sizeof n);
+    return le32toh(n);
+}
+
+static inline void
+ovs_store_le32(uint8_t *p, uint32_t v)
+{
+    uint32_t n = htole32(v);
+    memcpy(p, &n, sizeof n);
+}
+
 /* These macros may substitute for htons(), htonl(), and htonll() in contexts
  * where function calls are not allowed, such as case labels.  They should not
  * be used elsewhere because all of them evaluate their argument many times. */
-- 
2.55.0

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to