Refactor: Replace custom tc_hton*/tc_ntoh* with standard htobe16/be16toh/htobe32/be32toh, move htobe16/be16toh to platform_compat.h

This commit is contained in:
Evgeny committed 2026-04-25 01:16:09 +03:00
1 parent 69b49d424b
commit 595d2b177b
5 files changed
+67 -81

No files matched your search

+2 -13
View File
@@ -6,12 +6,6 @@
#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 <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
@@ -23,6 +17,8 @@
// Byte order conversion functions
#ifdef _WIN32
#include <stdlib.h>
#define htobe16(x) _byteswap_ushort(x)
#define be16toh(x) _byteswap_ushort(x)
#define htobe32(x) _byteswap_ulong(x)
#define be32toh(x) _byteswap_ulong(x)
#define htobe64(x) _byteswap_uint64(x)
@@ -159,13 +155,6 @@
#include <stdint.h>
#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
int random_bytes(uint8_t *buffer, size_t len);