diff --git a/lib/platform_compat.h b/lib/platform_compat.h index eba19429..821b03de 100644 --- a/lib/platform_compat.h +++ b/lib/platform_compat.h @@ -6,6 +6,12 @@ #define PLATFORM_COMPAT_H #ifdef _WIN32 + // Portable byte-order: must be first, before winsock2.h defines anything + #define tc_htons(x) ((uint16_t)(((uint16_t)(x) & 0xFFU) << 8) | (((uint16_t)(x) & 0xFF00U) >> 8)) + #define tc_htonl(x) ((((uint32_t)(x) & 0x000000FFU) << 24) | (((uint32_t)(x) & 0x0000FF00U) << 8) | (((uint32_t)(x) & 0x00FF0000U) >> 8) | (((uint32_t)(x) & 0xFF000000U) >> 24)) + #define tc_ntohs(x) tc_htons(x) + #define tc_ntohl(x) tc_htonl(x) + #include #include #include @@ -153,11 +159,12 @@ #include -// Portable byte-order: no winsock2, no struct in_addr, pure C +#ifndef tc_htons static inline uint16_t tc_htons(uint16_t x) { return (uint16_t)(((x & 0xFFU) << 8) | ((x & 0xFF00U) >> 8)); } static inline uint32_t tc_htonl(uint32_t x) { return (((x & 0x000000FFU) << 24) | ((x & 0x0000FF00U) << 8) | ((x & 0x00FF0000U) >> 8) | ((x & 0xFF000000U) >> 24)); } #define tc_ntohs(x) tc_htons(x) #define tc_ntohl(x) tc_htonl(x) +#endif // Generate cryptographically secure random bytes // Returns 0 on success, -1 on error