From fba15dc72c9459499f956ddc637eb849564f7f96 Mon Sep 17 00:00:00 2001
From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
Date: Mon, 14 Oct 2019 13:32:56 +0900
Subject: [PATCH] Use endian.h

---
 CMakeLists.txt    | 11 +++++++++++
 cmakeconfig.h.in  |  9 +++++++++
 configure.ac      | 12 ++++++++++++
 lib/ngtcp2_conv.h | 18 +++++++++++++-----
 lib/ngtcp2_map.c  |  4 +---
 5 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 24f665f3..dd2cf62c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -99,6 +99,8 @@ check_include_file("stdint.h"      HAVE_STDINT_H)
 check_include_file("stdlib.h"      HAVE_STDLIB_H)
 check_include_file("string.h"      HAVE_STRING_H)
 check_include_file("unistd.h"      HAVE_UNISTD_H)
+check_include_file("sys/endian.h"  HAVE_SYS_ENDIAN_H)
+check_include_file("endian.h"      HAVE_ENDIAN_H)
 
 include(CheckTypeSize)
 # Checks for typedefs, structures, and compiler characteristics.
@@ -110,6 +112,15 @@ if(SIZEOF_SSIZE_T STREQUAL "")
   set(ssize_t int)
 endif()
 
+# Checks for symbols.
+include(CheckSymbolExists)
+if(HAVE_ENDIAN_H)
+  check_symbol_exists(be64toh "endian.h" HAVE_BE64TOH)
+endif()
+if(HAVE_SYS_ENDIAN_H)
+  check_symbol_exists(be64toh "sys/endian.h" HAVE_BE64TOH)
+endif()
+
 include(ExtractValidFlags)
 set(WARNCFLAGS)
 set(WARNCXXFLAGS)
diff --git a/cmakeconfig.h.in b/cmakeconfig.h.in
index 4812069e..011f9b93 100644
--- a/cmakeconfig.h.in
+++ b/cmakeconfig.h.in
@@ -25,3 +25,12 @@
 
 /* Define to 1 if you have the <unistd.h> header file. */
 #cmakedefine HAVE_UNISTD_H 1
+
+/* Define to 1 if you have the <sys/endian.h> header file. */
+#cmakedefine HAVE_SYS_ENDIAN_H 1
+
+/* Define to 1 if you have the <endian.h> header file. */
+#cmakedefine HAVE_ENDIAN_H 1
+
+/* Define to 1 if you have the `be64toh' function. */
+#cmakedefine HAVE_BE64TOH 1
diff --git a/configure.ac b/configure.ac
index 56783ce1..6f2b9b8a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -204,6 +204,8 @@ AC_CHECK_HEADERS([ \
   stdlib.h \
   string.h \
   unistd.h \
+  sys/endian.h \
+  endian.h \
 ])
 
 # Checks for typedefs, structures, and compiler characteristics.
@@ -231,6 +233,16 @@ AC_CHECK_FUNCS([ \
   memset \
 ])
 
+# Checks for symbols.
+AC_CHECK_DECLS([be64toh], [], [], [[
+#ifdef HAVE_ENDIAN_H
+#  include <endian.h>
+#endif
+#ifdef HAVE_SYS_ENDIAN_H
+#  include <sys/endian.h>
+#endif
+]])
+
 # More compiler flags from nghttp2.
 save_CFLAGS=$CFLAGS
 save_CXXFLAGS=$CXXFLAGS
diff --git a/lib/ngtcp2_conv.h b/lib/ngtcp2_conv.h
index d902aede..be2004b9 100644
--- a/lib/ngtcp2_conv.h
+++ b/lib/ngtcp2_conv.h
@@ -33,17 +33,25 @@
 #  include <arpa/inet.h>
 #endif /* HAVE_ARPA_INET_H */
 
+#ifdef HAVE_ENDIAN_H
+#  include <endian.h>
+#endif /* HAVE_ENDIAN_H */
+
+#ifdef HAVE_SYS_ENDIAN_H
+#  include <sys/endian.h>
+#endif /* HAVE_SYS_ENDIAN_H */
+
 #include <ngtcp2/ngtcp2.h>
 
-#ifdef WORDS_BIGENDIAN
-#  define ngtcp2_ntohl64(N) (N)
-#  define ngtcp2_htonl64(N) (N)
-#else /* !WORDS_BIGENDIAN */
+#if defined HAVE_BE64TOH || HAVE_DECL_BE64TOH
+#  define ngtcp2_ntohl64(N) be64toh(N)
+#  define ngtcp2_htonl64(N) htobe64(N)
+#else /* !HAVE_BE64TOH */
 #  define ngtcp2_bswap64(N)                                                    \
     ((uint64_t)(ntohl((uint32_t)(N))) << 32 | ntohl((uint32_t)((N) >> 32)))
 #  define ngtcp2_ntohl64(N) ngtcp2_bswap64(N)
 #  define ngtcp2_htonl64(N) ngtcp2_bswap64(N)
-#endif /* !WORDS_BIGENDIAN */
+#endif /* !HAVE_BE64TOH */
 
 /*
  * ngtcp2_get_uint64 reads 8 bytes from |p| as 64 bits unsigned
diff --git a/lib/ngtcp2_map.c b/lib/ngtcp2_map.c
index 756b3ffe..84328a73 100644
--- a/lib/ngtcp2_map.c
+++ b/lib/ngtcp2_map.c
@@ -92,9 +92,7 @@ static uint32_t hash(key_type key, uint32_t mod) {
   uint8_t *p, *end;
   uint32_t h = 0x811C9DC5u;
 
-#ifndef WORDS_BIGENDIAN
-  key = ngtcp2_bswap64(key);
-#endif /* !WORDS_BIGENDIAN */
+  key = ngtcp2_htonl64(key);
   p = (uint8_t *)&key;
   end = p + sizeof(key_type);
 
-- 
GitLab