diff options
Diffstat (limited to '')
-rw-r--r-- | crypto/compat/posix_win.c | 91 |
1 files changed, 37 insertions, 54 deletions
diff --git a/crypto/compat/posix_win.c b/crypto/compat/posix_win.c index bb3e653..572e527 100644 --- a/crypto/compat/posix_win.c +++ b/crypto/compat/posix_win.c | |||
@@ -9,6 +9,8 @@ | |||
9 | 9 | ||
10 | #define NO_REDEF_POSIX_FUNCTIONS | 10 | #define NO_REDEF_POSIX_FUNCTIONS |
11 | 11 | ||
12 | #include <sys/time.h> | ||
13 | |||
12 | #include <ws2tcpip.h> | 14 | #include <ws2tcpip.h> |
13 | #include <windows.h> | 15 | #include <windows.h> |
14 | 16 | ||
@@ -20,6 +22,25 @@ | |||
20 | #include <string.h> | 22 | #include <string.h> |
21 | #include <unistd.h> | 23 | #include <unistd.h> |
22 | 24 | ||
25 | #include <sys/stat.h> | ||
26 | |||
27 | static int | ||
28 | is_socket(int fd) | ||
29 | { | ||
30 | // Border case: Don't break std* file descriptors | ||
31 | if (fd < 3) | ||
32 | return 0; | ||
33 | |||
34 | // All locally-allocated file descriptors will have the high bit set | ||
35 | return (fd & 0x80000000) == 0; | ||
36 | } | ||
37 | |||
38 | static int | ||
39 | get_real_fd(int fd) | ||
40 | { | ||
41 | return (fd & 0x7fffffff); | ||
42 | } | ||
43 | |||
23 | void | 44 | void |
24 | posix_perror(const char *s) | 45 | posix_perror(const char *s) |
25 | { | 46 | { |
@@ -42,6 +63,12 @@ posix_fopen(const char *path, const char *mode) | |||
42 | } | 63 | } |
43 | 64 | ||
44 | int | 65 | int |
66 | libressl_fstat(int fd, struct stat *statbuf) | ||
67 | { | ||
68 | return fstat(get_real_fd(fd), statbuf); | ||
69 | } | ||
70 | |||
71 | int | ||
45 | posix_open(const char *path, ...) | 72 | posix_open(const char *path, ...) |
46 | { | 73 | { |
47 | va_list ap; | 74 | va_list ap; |
@@ -60,7 +87,11 @@ posix_open(const char *path, ...) | |||
60 | flags |= O_NOINHERIT; | 87 | flags |= O_NOINHERIT; |
61 | } | 88 | } |
62 | flags &= ~O_NONBLOCK; | 89 | flags &= ~O_NONBLOCK; |
63 | return open(path, flags, mode); | 90 | |
91 | const int fh = open(path, flags, mode); | ||
92 | |||
93 | // Set high bit to mark file descriptor as a file handle | ||
94 | return fh + 0x80000000; | ||
64 | } | 95 | } |
65 | 96 | ||
66 | char * | 97 | char * |
@@ -148,52 +179,6 @@ wsa_errno(int err) | |||
148 | return -1; | 179 | return -1; |
149 | } | 180 | } |
150 | 181 | ||
151 | /* | ||
152 | * Employ a similar trick to cpython (pycore_fileutils.h) where the CRT report | ||
153 | * handler is disabled while checking if a descriptor is a socket or a file | ||
154 | */ | ||
155 | #if defined _MSC_VER && _MSC_VER >= 1900 | ||
156 | |||
157 | #include <crtdbg.h> | ||
158 | #include <stdlib.h> | ||
159 | |||
160 | static void noop_handler(const wchar_t *expression, const wchar_t *function, | ||
161 | const wchar_t *file, unsigned int line, uintptr_t pReserved) | ||
162 | { | ||
163 | return; | ||
164 | } | ||
165 | |||
166 | #define BEGIN_SUPPRESS_IPH \ | ||
167 | const int old_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0); \ | ||
168 | const _invalid_parameter_handler old_handler = _set_thread_local_invalid_parameter_handler(noop_handler) | ||
169 | #define END_SUPPRESS_IPH \ | ||
170 | (void)old_report_mode; /* Silence warning in release mode when _CrtSetReportMode compiles to void. */ \ | ||
171 | _CrtSetReportMode(_CRT_ASSERT, old_report_mode); \ | ||
172 | _set_thread_local_invalid_parameter_handler(old_handler) | ||
173 | |||
174 | #else | ||
175 | |||
176 | #define BEGIN_SUPPRESS_IPH | ||
177 | #define END_SUPPRESS_IPH | ||
178 | |||
179 | #endif | ||
180 | |||
181 | static int | ||
182 | is_socket(int fd) | ||
183 | { | ||
184 | intptr_t hd; | ||
185 | |||
186 | BEGIN_SUPPRESS_IPH; | ||
187 | hd = _get_osfhandle(fd); | ||
188 | END_SUPPRESS_IPH; | ||
189 | |||
190 | if (hd == (intptr_t)INVALID_HANDLE_VALUE) { | ||
191 | return 1; /* fd is not file descriptor */ | ||
192 | } | ||
193 | |||
194 | return 0; | ||
195 | } | ||
196 | |||
197 | int | 182 | int |
198 | posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) | 183 | posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) |
199 | { | 184 | { |
@@ -207,14 +192,13 @@ int | |||
207 | posix_close(int fd) | 192 | posix_close(int fd) |
208 | { | 193 | { |
209 | int rc; | 194 | int rc; |
210 | |||
211 | if (is_socket(fd)) { | 195 | if (is_socket(fd)) { |
212 | if ((rc = closesocket(fd)) == SOCKET_ERROR) { | 196 | if ((rc = closesocket(fd)) == SOCKET_ERROR) { |
213 | int err = WSAGetLastError(); | 197 | int err = WSAGetLastError(); |
214 | rc = wsa_errno(err); | 198 | rc = wsa_errno(err); |
215 | } | 199 | } |
216 | } else { | 200 | } else { |
217 | rc = close(fd); | 201 | rc = close(get_real_fd(fd)); |
218 | } | 202 | } |
219 | return rc; | 203 | return rc; |
220 | } | 204 | } |
@@ -223,14 +207,13 @@ ssize_t | |||
223 | posix_read(int fd, void *buf, size_t count) | 207 | posix_read(int fd, void *buf, size_t count) |
224 | { | 208 | { |
225 | ssize_t rc; | 209 | ssize_t rc; |
226 | |||
227 | if (is_socket(fd)) { | 210 | if (is_socket(fd)) { |
228 | if ((rc = recv(fd, buf, count, 0)) == SOCKET_ERROR) { | 211 | if ((rc = recv(fd, buf, count, 0)) == SOCKET_ERROR) { |
229 | int err = WSAGetLastError(); | 212 | int err = WSAGetLastError(); |
230 | rc = wsa_errno(err); | 213 | rc = wsa_errno(err); |
231 | } | 214 | } |
232 | } else { | 215 | } else { |
233 | rc = read(fd, buf, count); | 216 | rc = read(get_real_fd(fd), buf, count); |
234 | } | 217 | } |
235 | return rc; | 218 | return rc; |
236 | } | 219 | } |
@@ -244,7 +227,7 @@ posix_write(int fd, const void *buf, size_t count) | |||
244 | rc = wsa_errno(WSAGetLastError()); | 227 | rc = wsa_errno(WSAGetLastError()); |
245 | } | 228 | } |
246 | } else { | 229 | } else { |
247 | rc = write(fd, buf, count); | 230 | rc = write(get_real_fd(fd), buf, count); |
248 | } | 231 | } |
249 | return rc; | 232 | return rc; |
250 | } | 233 | } |
@@ -289,7 +272,7 @@ uid_t getuid(void) | |||
289 | 272 | ||
290 | #ifdef _MSC_VER | 273 | #ifdef _MSC_VER |
291 | struct timezone; | 274 | struct timezone; |
292 | int gettimeofday(struct timeval * tp, struct timezone * tzp) | 275 | int gettimeofday(struct timeval *tp, void *tzp) |
293 | { | 276 | { |
294 | /* | 277 | /* |
295 | * Note: some broken versions only have 8 trailing zero's, the correct | 278 | * Note: some broken versions only have 8 trailing zero's, the correct |
@@ -306,7 +289,7 @@ int gettimeofday(struct timeval * tp, struct timezone * tzp) | |||
306 | time = ((uint64_t)file_time.dwLowDateTime); | 289 | time = ((uint64_t)file_time.dwLowDateTime); |
307 | time += ((uint64_t)file_time.dwHighDateTime) << 32; | 290 | time += ((uint64_t)file_time.dwHighDateTime) << 32; |
308 | 291 | ||
309 | tp->tv_sec = (long)((time - EPOCH) / 10000000L); | 292 | tp->tv_sec = (long long)((time - EPOCH) / 10000000L); |
310 | tp->tv_usec = (long)(system_time.wMilliseconds * 1000); | 293 | tp->tv_usec = (long)(system_time.wMilliseconds * 1000); |
311 | return 0; | 294 | return 0; |
312 | } | 295 | } |