aboutsummaryrefslogtreecommitdiff
path: root/crypto/compat
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/compat')
-rw-r--r--crypto/compat/getdelim.c78
-rw-r--r--crypto/compat/getline.c40
-rw-r--r--crypto/compat/posix_win.c91
-rw-r--r--crypto/compat/ui_openssl_win.c4
4 files changed, 157 insertions, 56 deletions
diff --git a/crypto/compat/getdelim.c b/crypto/compat/getdelim.c
new file mode 100644
index 0000000..caec3f2
--- /dev/null
+++ b/crypto/compat/getdelim.c
@@ -0,0 +1,78 @@
1/*-
2 * Copyright (c) 2011 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Christos Zoulas.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32
33#ifndef HAVE_GETDELIM
34
35ssize_t
36getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
37{
38 char *ptr, *eptr;
39
40
41 if (*buf == NULL || *bufsiz == 0) {
42 *bufsiz = BUFSIZ;
43 if ((*buf = malloc(*bufsiz)) == NULL)
44 return -1;
45 }
46
47 for (ptr = *buf, eptr = *buf + *bufsiz;;) {
48 int c = fgetc(fp);
49 if (c == -1) {
50 if (feof(fp)) {
51 ssize_t diff = (ssize_t)(ptr - *buf);
52 if (diff != 0) {
53 *ptr = '\0';
54 return diff;
55 }
56 }
57 return -1;
58 }
59 *ptr++ = c;
60 if (c == delimiter) {
61 *ptr = '\0';
62 return ptr - *buf;
63 }
64 if (ptr + 2 >= eptr) {
65 char *nbuf;
66 size_t nbufsiz = *bufsiz * 2;
67 ssize_t d = ptr - *buf;
68 if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
69 return -1;
70 *buf = nbuf;
71 *bufsiz = nbufsiz;
72 eptr = nbuf + nbufsiz;
73 ptr = nbuf + d;
74 }
75 }
76}
77
78#endif /* HAVE_GETDELIM */
diff --git a/crypto/compat/getline.c b/crypto/compat/getline.c
new file mode 100644
index 0000000..e6ecde0
--- /dev/null
+++ b/crypto/compat/getline.c
@@ -0,0 +1,40 @@
1/*-
2 * Copyright (c) 2011 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Christos Zoulas.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdio.h>
31
32#ifndef HAVE_GETLINE
33
34ssize_t
35getline(char **buf, size_t *bufsiz, FILE *fp)
36{
37 return getdelim(buf, bufsiz, '\n', fp);
38}
39
40#endif /* HAVE_GETLINE */
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
27static int
28is_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
38static int
39get_real_fd(int fd)
40{
41 return (fd & 0x7fffffff);
42}
43
23void 44void
24posix_perror(const char *s) 45posix_perror(const char *s)
25{ 46{
@@ -42,6 +63,12 @@ posix_fopen(const char *path, const char *mode)
42} 63}
43 64
44int 65int
66libressl_fstat(int fd, struct stat *statbuf)
67{
68 return fstat(get_real_fd(fd), statbuf);
69}
70
71int
45posix_open(const char *path, ...) 72posix_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
66char * 97char *
@@ -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
160static 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
181static int
182is_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
197int 182int
198posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 183posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
199{ 184{
@@ -207,14 +192,13 @@ int
207posix_close(int fd) 192posix_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
223posix_read(int fd, void *buf, size_t count) 207posix_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
291struct timezone; 274struct timezone;
292int gettimeofday(struct timeval * tp, struct timezone * tzp) 275int 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}
diff --git a/crypto/compat/ui_openssl_win.c b/crypto/compat/ui_openssl_win.c
index 09705e4..4ae2c5d 100644
--- a/crypto/compat/ui_openssl_win.c
+++ b/crypto/compat/ui_openssl_win.c
@@ -146,7 +146,7 @@ static int echo_console(UI *ui);
146static int noecho_console(UI *ui); 146static int noecho_console(UI *ui);
147static int close_console(UI *ui); 147static int close_console(UI *ui);
148 148
149static UI_METHOD ui_openssl = { 149static const UI_METHOD ui_openssl = {
150 .name = "OpenSSL default user interface", 150 .name = "OpenSSL default user interface",
151 .ui_open_session = open_console, 151 .ui_open_session = open_console,
152 .ui_write_string = write_string, 152 .ui_write_string = write_string,
@@ -155,7 +155,7 @@ static UI_METHOD ui_openssl = {
155}; 155};
156 156
157/* The method with all the built-in thingies */ 157/* The method with all the built-in thingies */
158UI_METHOD * 158const UI_METHOD *
159UI_OpenSSL(void) 159UI_OpenSSL(void)
160{ 160{
161 return &ui_openssl; 161 return &ui_openssl;