Skip to content

Commit

Permalink
Utilize POSIX htonl/ntohl
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gbournou committed Jan 5, 2024
1 parent 7a4c5c5 commit ead8211
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 46 deletions.
2 changes: 1 addition & 1 deletion starboard/shared/linux/byte_swap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#if SB_API_VERSION < 16

#include "starboard/common/byte_swap.h"
#include "starboard/byte_swap.h"

#include <byteswap.h>

Expand Down
1 change: 0 additions & 1 deletion starboard/shared/linux/socket_get_interface_address.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <sys/socket.h>
#include <sys/types.h>

#include "starboard/common/byte_swap.h"
#include "starboard/common/log.h"
#include "starboard/memory.h"
#include "starboard/shared/posix/socket_internal.h"
Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/stub/byte_swap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/win32/byte_swap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#if SB_API_VERSION < 16

#include "starboard/common/byte_swap.h"
#include "starboard/byte_swap.h"

#include <stdlib.h>

Expand Down
1 change: 1 addition & 0 deletions starboard/shared/win32/posix_emu/include/arpa/inet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#if defined(STARBOARD) && defined(_MSC_VER)

#include <stdint.h>
#include <stdlib.h>

#include "starboard/configuration.h"
Expand Down
5 changes: 2 additions & 3 deletions starboard/shared/win32/socket_get_interface_address.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "starboard/common/socket.h"

#include <stdlib.h>
#include <winsock2.h>

#include <ifdef.h>
Expand All @@ -22,7 +23,6 @@
#include <algorithm>
#include <memory>

#include "starboard/common/byte_swap.h"
#include "starboard/common/log.h"
#include "starboard/memory.h"
#include "starboard/shared/win32/adapter_utils.h"
Expand Down Expand Up @@ -67,8 +67,7 @@ void GenerateNetMaskFromPrefixLength(UINT8 prefix_length,
UINT8 ones_in_this_dword = std::min<UINT8>(kBitsInOneDWORD, ones_left);
UINT64 mask_value =
kSbUInt64Max - ((1ULL << (kBitsInOneDWORD - ones_in_this_dword)) - 1);
*iterator =
SB_HOST_TO_NET_U32(static_cast<UINT32>(mask_value & kSbUInt64Max));
*iterator = _byteswap_ulong(static_cast<UINT32>(mask_value & kSbUInt64Max));
ones_left -= ones_in_this_dword;
}
}
Expand Down
11 changes: 5 additions & 6 deletions third_party/flac/src/libFLAC/bitreader.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
#else
#include <netinet/in.h> /* for ntohl() */
#endif
#endif // STARBOARD
#else // STARBOARD
#include <netinet/in.h> /* for ntohl() */
#include "starboard/client_porting/poem/stdio_poem.h"
#include "starboard/client_porting/poem/string_poem.h"
#endif // STARBOARD

#include <stdlib.h> /* for malloc() */

Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
6 changes: 2 additions & 4 deletions third_party/flac/src/libFLAC/bitwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <netinet/in.h> /* for ntohl() */
#endif
#else // STARBOARD
#include <netinet/in.h> /* for ntohl() */
#include "starboard/client_porting/poem/stdio_poem.h"
#endif // STARBOARD

Expand All @@ -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
Expand Down
8 changes: 1 addition & 7 deletions third_party/ots/include/opentype-sanitiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
typedef signed char int8_t;
typedef unsigned char uint8_t;
Expand Down
29 changes: 7 additions & 22 deletions third_party/woff2/src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
typedef signed char int8_t;
typedef unsigned char uint8_t;
Expand All @@ -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 <arpa/inet.h>
#include <stdint.h>
#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 <cstdio>
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -171,9 +161,4 @@ class Buffer {

} // namespace woff2

#undef woff2_ntohl
#undef woff2_ntohs
#undef woff2_htonl
#undef woff2_htons

#endif // WOFF2_BUFFER_H_

0 comments on commit ead8211

Please sign in to comment.