From c603018d4864251018c6d84689937d5a4d8b34a4 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 20 Aug 2024 16:00:02 +1000 Subject: [PATCH] use minimal winsock2 headers --- misc/windows/min-shellapi.h | 4 ++ misc/windows/min-wincrypt.h | 7 ++ misc/windows/min-winsock2.h | 139 ++++++++++++++++++++++++++++++++++++ src/Platform_Windows.c | 50 +++---------- 4 files changed, 161 insertions(+), 39 deletions(-) create mode 100644 misc/windows/min-shellapi.h create mode 100644 misc/windows/min-wincrypt.h create mode 100644 misc/windows/min-winsock2.h diff --git a/misc/windows/min-shellapi.h b/misc/windows/min-shellapi.h new file mode 100644 index 000000000..fef9cbf0d --- /dev/null +++ b/misc/windows/min-shellapi.h @@ -0,0 +1,4 @@ +#define SHELLAPI DECLSPEC_IMPORT + +SHELLAPI HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR operation, LPCWSTR file, LPCWSTR parameters, LPCWSTR directory, INT showCmd); +SHELLAPI HINSTANCE WINAPI ShellExecuteA(HWND hwnd, LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory, INT showCmd); \ No newline at end of file diff --git a/misc/windows/min-wincrypt.h b/misc/windows/min-wincrypt.h new file mode 100644 index 000000000..6a19839f5 --- /dev/null +++ b/misc/windows/min-wincrypt.h @@ -0,0 +1,7 @@ +typedef struct _CRYPTOAPI_BLOB { + DWORD cbData; + BYTE* pbData; +} DATA_BLOB; + +static BOOL (WINAPI *_CryptProtectData )(DATA_BLOB* dataIn, PCWSTR dataDescr, PVOID entropy, PVOID reserved, PVOID promptStruct, DWORD flags, DATA_BLOB* dataOut); +static BOOL (WINAPI *_CryptUnprotectData)(DATA_BLOB* dataIn, PWSTR* dataDescr, PVOID entropy, PVOID reserved, PVOID promptStruct, DWORD flags, DATA_BLOB* dataOut); \ No newline at end of file diff --git a/misc/windows/min-winsock2.h b/misc/windows/min-winsock2.h new file mode 100644 index 000000000..a9e2bb4a7 --- /dev/null +++ b/misc/windows/min-winsock2.h @@ -0,0 +1,139 @@ +/* IP addresses */ +typedef struct sockaddr { + USHORT sa_family; + char sa_data[14]; +} SOCKADDR; + +typedef struct in_addr { + union in_data { + struct { UCHAR s_b1,s_b2,s_b3,s_b4; } S_un_b; + struct { USHORT s_w1,s_w2; } S_un_w; + ULONG S_addr; + } S_un; +} IN_ADDR; + +typedef struct sockaddr_in { + SHORT sin_family; + USHORT sin_port; + struct in_addr sin_addr; + char sin_zero[8]; +} SOCKADDR_IN; + +typedef struct in6_addr { + union in6_data { + UCHAR Byte[16]; + USHORT Word[8]; + } u; +} IN6_ADDR; + +typedef struct sockaddr_in6 { + USHORT sin6_family; + USHORT sin6_port; + ULONG sin6_flowinfo; + IN6_ADDR sin6_addr; + ULONG sin6_scope_id; +} SOCKADDR_IN6; + +typedef struct sockaddr_storage { + short ss_family; + CHAR ss_pad[126]; +} SOCKADDR_STORAGE; + +/* Type declarations */ +typedef unsigned char u_char; +typedef unsigned short u_short; +typedef unsigned int u_int; +typedef unsigned long u_long; + +typedef UINT_PTR SOCKET; + +/* Constants */ +#define AF_INET 2 +#define AF_INET6 23 + +#define SOCK_STREAM 1 + +#define IPPROTO_TCP 6 + +#define SOL_SOCKET 0xffff + +#define SO_ERROR 0x1007 + +#define SD_BOTH 0x02 + +#define IOC_IN 0x80000000 +/* has input parameters | input size| func group | command */ +#define FIONBIO (IOC_IN | (4 << 16) | ('f' << 8) | 126) + +/* Struct declarations */ +#define FD_SETSIZE 64 +typedef struct fd_set { + u_int fd_count; + SOCKET fd_array[FD_SETSIZE]; +} fd_set; + +struct timeval { + long tv_sec; + long tv_usec; +}; + +struct hostent { + char* h_name; + char** h_aliases; + short h_addrtype; + short h_length; + char** h_addr_list; +}; + +typedef struct addrinfo { + int ai_flags; + int ai_family; + int ai_socktype; + int ai_protocol; + size_t ai_addrlen; + char * ai_canonname; + struct sockaddr * ai_addr; + struct addrinfo * ai_next; +} ADDRINFOA; + +#define WSADESCRIPTION_LEN 256 +#define WSASYS_STATUS_LEN 128 +typedef struct WSAData { + WORD wVersion; + WORD wHighVersion; +#ifdef _WIN64 + unsigned short iMaxSockets; + unsigned short iMaxUdpDg; + char* lpVendorInfo; + char szDescription[WSADESCRIPTION_LEN + 1]; + char szSystemStatus[WSASYS_STATUS_LEN + 1]; +#else + char szDescription[WSADESCRIPTION_LEN + 1]; + char szSystemStatus[WSASYS_STATUS_LEN + 1]; + unsigned short iMaxSockets; + unsigned short iMaxUdpDg; + char* lpVendorInfo; +#endif +} WSADATA; + +/* Function declarations */ +static int (WINAPI *_WSAStartup)(WORD versionRequested, WSADATA* wsaData); +static int (WINAPI *_WSACleanup)(void); +static int (WINAPI *_WSAGetLastError)(void); +static int (WINAPI *_WSAStringToAddressW)(LPWSTR addressString, INT addressFamily, LPVOID protocolInfo, LPVOID address, LPINT addressLength); + +static int (WINAPI *_socket)(int af, int type, int protocol); +static int (WINAPI *_closesocket)(SOCKET s); +static int (WINAPI *_connect)(SOCKET s, const struct sockaddr* name, int namelen); +static int (WINAPI *_shutdown)(SOCKET s, int how); + +static int (WINAPI *_ioctlsocket)(SOCKET s, long cmd, u_long* argp); +static int (WINAPI *_getsockopt)(SOCKET s, int level, int optname, char* optval, int* optlen); +static int (WINAPI *_recv)(SOCKET s, char* buf, int len, int flags); +static int (WINAPI *_send)(SOCKET s, const char FAR * buf, int len, int flags); +static int (WINAPI *_select)(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const struct timeval* timeout); + +static struct hostent* (WINAPI *_gethostbyname)(const char* name); +static unsigned short (WINAPI *_htons)(u_short hostshort); +static int (WINAPI *_getaddrinfo )(PCSTR nodeName, PCSTR serviceName, const ADDRINFOA* hints, ADDRINFOA** result); +static void (WINAPI* _freeaddrinfo)(ADDRINFOA* addrInfo); diff --git a/src/Platform_Windows.c b/src/Platform_Windows.c index 26d3a16d4..32e98832c 100644 --- a/src/Platform_Windows.c +++ b/src/Platform_Windows.c @@ -16,23 +16,17 @@ #define UNICODE #define _UNICODE #endif -#include /* auto includes windows.h */ +#include +/* +#include #include - -/* === BEGIN shellapi.h === */ -#define SHELLAPI DECLSPEC_IMPORT -SHELLAPI HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR operation, LPCWSTR file, LPCWSTR parameters, LPCWSTR directory, INT showCmd); -SHELLAPI HINSTANCE WINAPI ShellExecuteA(HWND hwnd, LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory, INT showCmd); -/* === END shellapi.h === */ -/* === BEGIN wincrypt.h === */ -typedef struct _CRYPTOAPI_BLOB { - DWORD cbData; - BYTE* pbData; -} DATA_BLOB; - -static BOOL (WINAPI *_CryptProtectData )(DATA_BLOB* dataIn, PCWSTR dataDescr, PVOID entropy, PVOID reserved, PVOID promptStruct, DWORD flags, DATA_BLOB* dataOut); -static BOOL (WINAPI *_CryptUnprotectData)(DATA_BLOB* dataIn, PWSTR* dataDescr, PVOID entropy, PVOID reserved, PVOID promptStruct, DWORD flags, DATA_BLOB* dataOut); -/* === END wincrypt.h === */ +#include +#include +*/ +/* Compatibility versions so compiling works on older Windows SDKs */ +#include "../misc/windows/min-winsock2.h" +#include "../misc/windows/min-shellapi.h" +#include "../misc/windows/min-wincrypt.h" static HANDLE heap; const cc_result ReturnCode_FileShareViolation = ERROR_SHARING_VIOLATION; @@ -448,28 +442,6 @@ void Platform_LoadSysFonts(void) { /* Sanity check to ensure cc_sockaddr struct is large enough to contain all socket addresses supported by this platform */ static char sockaddr_size_check[sizeof(SOCKADDR_STORAGE) < CC_SOCKETADDR_MAXSIZE ? 1 : -1]; -static int (WINAPI *_WSAStartup)(WORD versionRequested, LPWSADATA wsaData); -static int (WINAPI *_WSACleanup)(void); -static int (WINAPI *_WSAGetLastError)(void); -static int (WINAPI *_WSAStringToAddressW)(LPWSTR addressString, INT addressFamily, LPVOID protocolInfo, LPVOID address, LPINT addressLength); - -static int (WINAPI *_socket)(int af, int type, int protocol); -static int (WINAPI *_closesocket)(SOCKET s); -static int (WINAPI *_connect)(SOCKET s, const struct sockaddr* name, int namelen); -static int (WINAPI *_shutdown)(SOCKET s, int how); - -static int (WINAPI *_ioctlsocket)(SOCKET s, long cmd, u_long* argp); -static int (WINAPI *_getsockopt)(SOCKET s, int level, int optname, char* optval, int* optlen); -static int (WINAPI *_recv)(SOCKET s, char* buf, int len, int flags); -static int (WINAPI *_send)(SOCKET s, const char FAR * buf, int len, int flags); -static int (WINAPI *_select)(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const struct timeval* timeout); - -static struct hostent* (WINAPI *_gethostbyname)(const char* name); -static unsigned short (WINAPI *_htons)(u_short hostshort); -static int (WINAPI *_getaddrinfo )(PCSTR nodeName, PCSTR serviceName, const ADDRINFOA* hints, PADDRINFOA* result); -static void (WINAPI* _freeaddrinfo)(PADDRINFOA addrInfo); - - static INT WINAPI FallbackParseAddress(LPWSTR addressString, INT addressFamily, LPVOID protocolInfo, LPVOID address, LPINT addressLength) { SOCKADDR_IN* addr4 = (SOCKADDR_IN*)address; @@ -565,7 +537,7 @@ static cc_result ParseHostNew(char* host, int port, cc_sockaddr* addrs, int* num portRaw[portStr.length] = '\0'; res = _getaddrinfo(host, portRaw, &hints, &result); - if (res == EAI_NONAME) return SOCK_ERR_UNKNOWN_HOST; + if (res == WSAHOST_NOT_FOUND) return SOCK_ERR_UNKNOWN_HOST; if (res) return res; /* Prefer IPv4 addresses first */