From 47fbcea542f563395d77e6d483dbcc3a00ff2eaa Mon Sep 17 00:00:00 2001
From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
Date: Mon, 14 Oct 2019 12:33:11 +0900
Subject: [PATCH] Define ngtcp2_ntohl64 and ngtcp2_htonl64

---
 lib/ngtcp2_conv.c   | 10 +++++-----
 lib/ngtcp2_conv.h   |  7 +++++--
 lib/ngtcp2_crypto.c |  2 +-
 lib/ngtcp2_map.c    |  4 +++-
 4 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/lib/ngtcp2_conv.c b/lib/ngtcp2_conv.c
index 319dd14a..53184685 100644
--- a/lib/ngtcp2_conv.c
+++ b/lib/ngtcp2_conv.c
@@ -34,13 +34,13 @@
 uint64_t ngtcp2_get_uint64(const uint8_t *p) {
   uint64_t n;
   memcpy(&n, p, 8);
-  return bswap64(n);
+  return ngtcp2_ntohl64(n);
 }
 
 uint64_t ngtcp2_get_uint48(const uint8_t *p) {
   uint64_t n = 0;
   memcpy(((uint8_t *)&n) + 2, p, 6);
-  return bswap64(n);
+  return ngtcp2_ntohl64(n);
 }
 
 uint32_t ngtcp2_get_uint32(const uint8_t *p) {
@@ -85,7 +85,7 @@ uint64_t ngtcp2_get_varint(size_t *plen, const uint8_t *p) {
   case 8:
     memcpy(&n, p, 8);
     n.b[0] &= 0x3f;
-    return bswap64(n.n64);
+    return ngtcp2_ntohl64(n.n64);
   }
 
   assert(0);
@@ -107,12 +107,12 @@ int64_t ngtcp2_get_pkt_num(const uint8_t *p, size_t pkt_numlen) {
 }
 
 uint8_t *ngtcp2_put_uint64be(uint8_t *p, uint64_t n) {
-  n = bswap64(n);
+  n = ngtcp2_htonl64(n);
   return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
 }
 
 uint8_t *ngtcp2_put_uint48be(uint8_t *p, uint64_t n) {
-  n = bswap64(n);
+  n = ngtcp2_htonl64(n);
   return ngtcp2_cpymem(p, ((const uint8_t *)&n) + 2, 6);
 }
 
diff --git a/lib/ngtcp2_conv.h b/lib/ngtcp2_conv.h
index c70d42cf..d902aede 100644
--- a/lib/ngtcp2_conv.h
+++ b/lib/ngtcp2_conv.h
@@ -36,10 +36,13 @@
 #include <ngtcp2/ngtcp2.h>
 
 #ifdef WORDS_BIGENDIAN
-#  define bswap64(N) (N)
+#  define ngtcp2_ntohl64(N) (N)
+#  define ngtcp2_htonl64(N) (N)
 #else /* !WORDS_BIGENDIAN */
-#  define bswap64(N)                                                           \
+#  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 */
 
 /*
diff --git a/lib/ngtcp2_crypto.c b/lib/ngtcp2_crypto.c
index 40248126..0bdbedaa 100644
--- a/lib/ngtcp2_crypto.c
+++ b/lib/ngtcp2_crypto.c
@@ -72,7 +72,7 @@ void ngtcp2_crypto_create_nonce(uint8_t *dest, const uint8_t *iv, size_t ivlen,
   uint64_t n;
 
   memcpy(dest, iv, ivlen);
-  n = bswap64((uint64_t)pkt_num);
+  n = ngtcp2_htonl64((uint64_t)pkt_num);
 
   for (i = 0; i < 8; ++i) {
     dest[ivlen - 8 + i] ^= ((uint8_t *)&n)[i];
diff --git a/lib/ngtcp2_map.c b/lib/ngtcp2_map.c
index fd4757db..756b3ffe 100644
--- a/lib/ngtcp2_map.c
+++ b/lib/ngtcp2_map.c
@@ -92,7 +92,9 @@ static uint32_t hash(key_type key, uint32_t mod) {
   uint8_t *p, *end;
   uint32_t h = 0x811C9DC5u;
 
-  key = bswap64(key);
+#ifndef WORDS_BIGENDIAN
+  key = ngtcp2_bswap64(key);
+#endif /* !WORDS_BIGENDIAN */
   p = (uint8_t *)&key;
   end = p + sizeof(key_type);
 
-- 
GitLab