Skip to content

Commit b53bb39

Browse files
committed
Attempt to fix htonll/ntohll another way on Windows
1 parent 851eba2 commit b53bb39

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

php_crc_fast.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,29 @@ extern "C" {
1919

2020
// Define htonll/ntohll for platforms that don't provide them
2121
#if defined(_WIN32) || defined(_WIN64)
22-
// Windows SDK 10.0.26100.0+ provides htonll/ntohll in winsock2.h
23-
// For older SDK versions, we need to define them ourselves
22+
// Windows: winsock2.h provides htonll/ntohll in SDK 10.0.26100.0+
2423
#include <winsock2.h>
2524

26-
// Check if we're using an older Windows SDK that doesn't have these functions
27-
// The functions were added in Windows SDK 10.0.26100.0
28-
#if !defined(NTDDI_WIN10_NI) || NTDDI_VERSION < NTDDI_WIN10_NI
29-
#ifndef htonll
30-
inline uint64_t htonll(uint64_t x) {
31-
return (((uint64_t)htonl((uint32_t)(x & 0xFFFFFFFF))) << 32) | htonl((uint32_t)(x >> 32));
32-
}
33-
#endif
34-
#ifndef ntohll
35-
inline uint64_t ntohll(uint64_t x) {
36-
return (((uint64_t)ntohl((uint32_t)(x & 0xFFFFFFFF))) << 32) | ntohl((uint32_t)(x >> 32));
37-
}
38-
#endif
25+
// For older Windows SDKs that don't have htonll/ntohll, provide fallback
26+
// We use different function names to avoid conflicts with newer SDKs
27+
namespace {
28+
inline uint64_t php_htonll_fallback(uint64_t x) {
29+
return (((uint64_t)htonl((uint32_t)(x & 0xFFFFFFFF))) << 32) | htonl((uint32_t)(x >> 32));
30+
}
31+
inline uint64_t php_ntohll_fallback(uint64_t x) {
32+
return (((uint64_t)ntohl((uint32_t)(x & 0xFFFFFFFF))) << 32) | ntohl((uint32_t)(x >> 32));
33+
}
34+
}
35+
36+
// Check if we have the native functions (SDK 10.0.26100.0+)
37+
#if defined(NTDDI_VERSION) && defined(NTDDI_WIN10_NI) && (NTDDI_VERSION >= NTDDI_WIN10_NI)
38+
// Use the native functions from winsock2.h
39+
#define PHP_HTONLL(x) htonll(x)
40+
#define PHP_NTOHLL(x) ntohll(x)
41+
#else
42+
// Use our fallback implementations
43+
#define PHP_HTONLL(x) php_htonll_fallback(x)
44+
#define PHP_NTOHLL(x) php_ntohll_fallback(x)
3945
#endif
4046
#else
4147
// Unix-like platforms
@@ -50,6 +56,8 @@ extern "C" {
5056
#define ntohll(x) (x)
5157
#endif
5258
#endif
59+
#define PHP_HTONLL(x) htonll(x)
60+
#define PHP_NTOHLL(x) ntohll(x)
5361
#endif
5462

5563
/* For compatibility with older PHP versions */
@@ -150,7 +158,7 @@ static inline void php_crc_fast_format_result(INTERNAL_FUNCTION_PARAMETERS, zend
150158
RETURN_STRINGL((char*)&result32, sizeof(result32));
151159
} else {
152160
// 64-bit CRC
153-
result = htonll(result);
161+
result = PHP_HTONLL(result);
154162
RETURN_STRINGL((char*)&result, sizeof(result));
155163
}
156164
} else {
@@ -479,7 +487,7 @@ PHP_FUNCTION(CrcFast_combine)
479487
if (is_crc32) {
480488
cs1 = ntohl(*((uint32_t*)checksum1));
481489
} else {
482-
cs1 = ntohll(*((uint64_t*)checksum1));
490+
cs1 = PHP_NTOHLL(*((uint64_t*)checksum1));
483491
}
484492
} else if (checksum1_len == expected_hex_size) {
485493
// Hex input
@@ -525,7 +533,7 @@ PHP_FUNCTION(CrcFast_combine)
525533
if (is_crc32) {
526534
cs2 = ntohl(*((uint32_t*)checksum2));
527535
} else {
528-
cs2 = ntohll(*((uint64_t*)checksum2));
536+
cs2 = PHP_NTOHLL(*((uint64_t*)checksum2));
529537
}
530538
} else if (checksum2_len == expected_hex_size) {
531539
// Hex input

0 commit comments

Comments
 (0)