From ead8211f7718853cc9e8e09eb5b3b151f1c863a8 Mon Sep 17 00:00:00 2001 From: Garo Bournoutian Date: Fri, 5 Jan 2024 12:19:24 -0800 Subject: [PATCH] Utilize POSIX htonl/ntohl - Fix missing include - Remove Starboard customizations to third_party code - Prepare for removal of SB_HOST_TO_NET/SB_NET_TO_HOST macros b/317437129 Test-On-Device: true --- starboard/shared/linux/byte_swap.cc | 2 +- .../linux/socket_get_interface_address.cc | 1 - starboard/shared/stub/byte_swap.cc | 2 +- starboard/shared/win32/byte_swap.cc | 2 +- .../win32/posix_emu/include/arpa/inet.h | 1 + .../win32/socket_get_interface_address.cc | 5 ++-- third_party/flac/src/libFLAC/bitreader.c | 11 ++++--- third_party/flac/src/libFLAC/bitwriter.c | 6 ++-- third_party/ots/include/opentype-sanitiser.h | 8 +---- third_party/woff2/src/buffer.h | 29 +++++-------------- 10 files changed, 21 insertions(+), 46 deletions(-) diff --git a/starboard/shared/linux/byte_swap.cc b/starboard/shared/linux/byte_swap.cc index 3661c778d94f..b2a104c142b2 100644 --- a/starboard/shared/linux/byte_swap.cc +++ b/starboard/shared/linux/byte_swap.cc @@ -17,7 +17,7 @@ #if SB_API_VERSION < 16 -#include "starboard/common/byte_swap.h" +#include "starboard/byte_swap.h" #include diff --git a/starboard/shared/linux/socket_get_interface_address.cc b/starboard/shared/linux/socket_get_interface_address.cc index fbc82d0a2ff6..ec4d7e95023f 100644 --- a/starboard/shared/linux/socket_get_interface_address.cc +++ b/starboard/shared/linux/socket_get_interface_address.cc @@ -34,7 +34,6 @@ #include #include -#include "starboard/common/byte_swap.h" #include "starboard/common/log.h" #include "starboard/memory.h" #include "starboard/shared/posix/socket_internal.h" diff --git a/starboard/shared/stub/byte_swap.cc b/starboard/shared/stub/byte_swap.cc index 6a7cef8cfac3..a64b2020eb0e 100644 --- a/starboard/shared/stub/byte_swap.cc +++ b/starboard/shared/stub/byte_swap.cc @@ -17,7 +17,7 @@ #if SB_API_VERSION < 16 -#include "starboard/common/byte_swap.h" +#include "starboard/byte_swap.h" int16_t SbByteSwapS16(int16_t value) { return 0; diff --git a/starboard/shared/win32/byte_swap.cc b/starboard/shared/win32/byte_swap.cc index 1394cea020ac..649ab6f73add 100644 --- a/starboard/shared/win32/byte_swap.cc +++ b/starboard/shared/win32/byte_swap.cc @@ -17,7 +17,7 @@ #if SB_API_VERSION < 16 -#include "starboard/common/byte_swap.h" +#include "starboard/byte_swap.h" #include diff --git a/starboard/shared/win32/posix_emu/include/arpa/inet.h b/starboard/shared/win32/posix_emu/include/arpa/inet.h index ac097c6d9838..58c8ba67f9cc 100644 --- a/starboard/shared/win32/posix_emu/include/arpa/inet.h +++ b/starboard/shared/win32/posix_emu/include/arpa/inet.h @@ -17,6 +17,7 @@ #if defined(STARBOARD) && defined(_MSC_VER) +#include #include #include "starboard/configuration.h" diff --git a/starboard/shared/win32/socket_get_interface_address.cc b/starboard/shared/win32/socket_get_interface_address.cc index ed0ef19a369d..c88f6feebd5b 100644 --- a/starboard/shared/win32/socket_get_interface_address.cc +++ b/starboard/shared/win32/socket_get_interface_address.cc @@ -14,6 +14,7 @@ #include "starboard/common/socket.h" +#include #include #include @@ -22,7 +23,6 @@ #include #include -#include "starboard/common/byte_swap.h" #include "starboard/common/log.h" #include "starboard/memory.h" #include "starboard/shared/win32/adapter_utils.h" @@ -67,8 +67,7 @@ void GenerateNetMaskFromPrefixLength(UINT8 prefix_length, UINT8 ones_in_this_dword = std::min(kBitsInOneDWORD, ones_left); UINT64 mask_value = kSbUInt64Max - ((1ULL << (kBitsInOneDWORD - ones_in_this_dword)) - 1); - *iterator = - SB_HOST_TO_NET_U32(static_cast(mask_value & kSbUInt64Max)); + *iterator = _byteswap_ulong(static_cast(mask_value & kSbUInt64Max)); ones_left -= ones_in_this_dword; } } diff --git a/third_party/flac/src/libFLAC/bitreader.c b/third_party/flac/src/libFLAC/bitreader.c index 1215863013e9..c669bb76a3c1 100644 --- a/third_party/flac/src/libFLAC/bitreader.c +++ b/third_party/flac/src/libFLAC/bitreader.c @@ -44,9 +44,11 @@ #else #include /* for ntohl() */ #endif -#endif // STARBOARD +#else // STARBOARD +#include /* for ntohl() */ #include "starboard/client_porting/poem/stdio_poem.h" #include "starboard/client_porting/poem/string_poem.h" +#endif // STARBOARD #include /* for malloc() */ @@ -67,11 +69,8 @@ typedef FLAC__uint32 brword; #if WORDS_BIGENDIAN #define SWAP_BE_WORD_TO_HOST(x) (x) #else -#if defined(_MSC_VER) && !defined(COBALT) +#ifdef _MSC_VER #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x) -#elif defined(STARBOARD) -#include "starboard/common/byte_swap.h" -#define SWAP_BE_WORD_TO_HOST(x) SB_NET_TO_HOST_U32(x) #else #define SWAP_BE_WORD_TO_HOST(x) ntohl(x) #endif @@ -158,7 +157,7 @@ struct FLAC__BitReader { FLAC__CPUInfo cpu_info; }; -#if defined(_MSC_VER) && defined(HAVE_WINSOCK_H) && !defined(COBALT) +#ifdef _MSC_VER /* OPT: an MSVC built-in would be better */ static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x) { diff --git a/third_party/flac/src/libFLAC/bitwriter.c b/third_party/flac/src/libFLAC/bitwriter.c index b4ea67c6fcc7..7110c876ead3 100644 --- a/third_party/flac/src/libFLAC/bitwriter.c +++ b/third_party/flac/src/libFLAC/bitwriter.c @@ -44,6 +44,7 @@ #include /* for ntohl() */ #endif #else // STARBOARD +#include /* for ntohl() */ #include "starboard/client_porting/poem/stdio_poem.h" #endif // STARBOARD @@ -69,11 +70,8 @@ typedef FLAC__uint32 bwword; #if WORDS_BIGENDIAN #define SWAP_BE_WORD_TO_HOST(x) (x) #else -#if defined(_MSC_VER) && !defined(COBALT) +#ifdef _MSC_VER #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x) -#elif defined(STARBOARD) -#include "starboard/common/byte_swap.h" -#define SWAP_BE_WORD_TO_HOST(x) SB_NET_TO_HOST_U32(x) #else #define SWAP_BE_WORD_TO_HOST(x) ntohl(x) #endif diff --git a/third_party/ots/include/opentype-sanitiser.h b/third_party/ots/include/opentype-sanitiser.h index 324f451c3be5..df36fe345f39 100644 --- a/third_party/ots/include/opentype-sanitiser.h +++ b/third_party/ots/include/opentype-sanitiser.h @@ -5,13 +5,7 @@ #ifndef OPENTYPE_SANITISER_H_ #define OPENTYPE_SANITISER_H_ -#if defined(STARBOARD) -#include "starboard/common/byte_swap.h" -#define ots_ntohl(x) SB_NET_TO_HOST_U32(x) -#define ots_ntohs(x) SB_NET_TO_HOST_U16(x) -#define ots_htonl(x) SB_HOST_TO_NET_U32(x) -#define ots_htons(x) SB_HOST_TO_NET_U16(x) -#elif defined(_WIN32) +#if defined(_WIN32) #include typedef signed char int8_t; typedef unsigned char uint8_t; diff --git a/third_party/woff2/src/buffer.h b/third_party/woff2/src/buffer.h index 161433907bf2..7240e5181ced 100644 --- a/third_party/woff2/src/buffer.h +++ b/third_party/woff2/src/buffer.h @@ -10,13 +10,7 @@ #ifndef WOFF2_BUFFER_H_ #define WOFF2_BUFFER_H_ -#if defined(STARBOARD) -#include "starboard/common/byte_swap.h" -#define woff2_ntohl(x) SB_NET_TO_HOST_U32(x) -#define woff2_ntohs(x) SB_NET_TO_HOST_U16(x) -#define woff2_htonl(x) SB_HOST_TO_NET_U32(x) -#define woff2_htons(x) SB_HOST_TO_NET_U16(x) -#elif defined(_WIN32) +#if defined(_WIN32) #include typedef signed char int8_t; typedef unsigned char uint8_t; @@ -26,17 +20,13 @@ typedef int int32_t; typedef unsigned int uint32_t; typedef __int64 int64_t; typedef unsigned __int64 uint64_t; -#define woff2_ntohl(x) _byteswap_ulong (x) -#define woff2_ntohs(x) _byteswap_ushort (x) -#define woff2_htonl(x) _byteswap_ulong (x) -#define woff2_htons(x) _byteswap_ushort (x) +#define ntohl(x) _byteswap_ulong (x) +#define ntohs(x) _byteswap_ushort (x) +#define htonl(x) _byteswap_ulong (x) +#define htons(x) _byteswap_ushort (x) #else #include #include -#define woff2_ntohl(x) ntohl (x) -#define woff2_ntohs(x) ntohs (x) -#define woff2_htonl(x) htonl (x) -#define woff2_htons(x) htons (x) #endif #include @@ -105,7 +95,7 @@ class Buffer { return FONT_COMPRESSION_FAILURE(); } std::memcpy(value, buffer_ + offset_, sizeof(uint16_t)); - *value = woff2_ntohs(*value); + *value = ntohs(*value); offset_ += 2; return true; } @@ -130,7 +120,7 @@ class Buffer { return FONT_COMPRESSION_FAILURE(); } std::memcpy(value, buffer_ + offset_, sizeof(uint32_t)); - *value = woff2_ntohl(*value); + *value = ntohl(*value); offset_ += 4; return true; } @@ -171,9 +161,4 @@ class Buffer { } // namespace woff2 -#undef woff2_ntohl -#undef woff2_ntohs -#undef woff2_htonl -#undef woff2_htons - #endif // WOFF2_BUFFER_H_