diff options
author | Brent Cook <busterb@gmail.com> | 2014-07-10 22:06:10 -0500 |
---|---|---|
committer | Brent Cook <bcook@openbsd.org> | 2015-07-21 12:08:18 -0500 |
commit | 5d8a1cf7155130bd8101090d7e1d0c2f90d9b123 (patch) | |
tree | 286f7d12e3647f94bd1e6e8e180a4bf6215a0740 /include/compat | |
parent | 7a4a37cf596697ae96eeb1c555989e6d1a443187 (diff) | |
download | portable-5d8a1cf7155130bd8101090d7e1d0c2f90d9b123.tar.gz portable-5d8a1cf7155130bd8101090d7e1d0c2f90d9b123.tar.bz2 portable-5d8a1cf7155130bd8101090d7e1d0c2f90d9b123.zip |
add initial CMake and Visual Studio build support
This moves the compatibility include files from include to
include/compat so we can use the awful MS C compiler
<../include/> trick to emulate the GNU #include_next extension.
This also removes a few old compat files we do not need anymore.
Diffstat (limited to 'include/compat')
-rw-r--r-- | include/compat/arpa/inet.h | 19 | ||||
-rw-r--r-- | include/compat/arpa/nameser.h | 23 | ||||
-rw-r--r-- | include/compat/dirent.h | 17 | ||||
-rw-r--r-- | include/compat/dirent_msvc.h | 748 | ||||
-rw-r--r-- | include/compat/err.h | 33 | ||||
-rw-r--r-- | include/compat/machine/endian.h | 40 | ||||
-rw-r--r-- | include/compat/netdb.h | 10 | ||||
-rw-r--r-- | include/compat/netinet/in.h | 10 | ||||
-rw-r--r-- | include/compat/netinet/tcp.h | 10 | ||||
-rw-r--r-- | include/compat/poll.h | 63 | ||||
-rw-r--r-- | include/compat/stdio.h | 39 | ||||
-rw-r--r-- | include/compat/stdlib.h | 34 | ||||
-rw-r--r-- | include/compat/string.h | 82 | ||||
-rw-r--r-- | include/compat/sys/cdefs.h | 31 | ||||
-rw-r--r-- | include/compat/sys/ioctl.h | 11 | ||||
-rw-r--r-- | include/compat/sys/mman.h | 19 | ||||
-rw-r--r-- | include/compat/sys/param.h | 15 | ||||
-rw-r--r-- | include/compat/sys/select.h | 10 | ||||
-rw-r--r-- | include/compat/sys/socket.h | 10 | ||||
-rw-r--r-- | include/compat/sys/stat.h | 95 | ||||
-rw-r--r-- | include/compat/sys/time.h | 16 | ||||
-rw-r--r-- | include/compat/sys/types.h | 43 | ||||
-rw-r--r-- | include/compat/sys/uio.h | 17 | ||||
-rw-r--r-- | include/compat/time.h | 16 | ||||
-rw-r--r-- | include/compat/unistd.h | 36 | ||||
-rw-r--r-- | include/compat/win32netcompat.h | 48 |
26 files changed, 1495 insertions, 0 deletions
diff --git a/include/compat/arpa/inet.h b/include/compat/arpa/inet.h new file mode 100644 index 0000000..0cea8c4 --- /dev/null +++ b/include/compat/arpa/inet.h | |||
@@ -0,0 +1,19 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * arpa/inet.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <arpa/inet.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | |||
11 | #ifndef AI_ADDRCONFIG | ||
12 | #define AI_ADDRCONFIG 0x00000400 | ||
13 | #endif | ||
14 | |||
15 | #endif | ||
16 | |||
17 | #ifndef HAVE_INET_PTON | ||
18 | int inet_pton(int af, const char * src, void * dst); | ||
19 | #endif | ||
diff --git a/include/compat/arpa/nameser.h b/include/compat/arpa/nameser.h new file mode 100644 index 0000000..0126a60 --- /dev/null +++ b/include/compat/arpa/nameser.h | |||
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * arpa/inet.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <arpa/nameser.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | |||
11 | #ifndef INADDRSZ | ||
12 | #define INADDRSZ 4 | ||
13 | #endif | ||
14 | |||
15 | #ifndef IN6ADDRSZ | ||
16 | #define IN6ADDRSZ 16 | ||
17 | #endif | ||
18 | |||
19 | #ifndef INT16SZ | ||
20 | #define INT16SZ 2 | ||
21 | #endif | ||
22 | |||
23 | #endif | ||
diff --git a/include/compat/dirent.h b/include/compat/dirent.h new file mode 100644 index 0000000..753e4a0 --- /dev/null +++ b/include/compat/dirent.h | |||
@@ -0,0 +1,17 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * dirent.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_DIRENT_H | ||
7 | #define LIBCRYPTOCOMPAT_DIRENT_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <windows.h> | ||
11 | #include <dirent_msvc.h> | ||
12 | #else | ||
13 | #include_next <dirent.h> | ||
14 | #endif | ||
15 | |||
16 | #endif | ||
17 | |||
diff --git a/include/compat/dirent_msvc.h b/include/compat/dirent_msvc.h new file mode 100644 index 0000000..bf9cf1a --- /dev/null +++ b/include/compat/dirent_msvc.h | |||
@@ -0,0 +1,748 @@ | |||
1 | /* | ||
2 | * dirent.h - dirent API for Microsoft Visual Studio | ||
3 | * | ||
4 | * Copyright (C) 2006-2012 Toni Ronkko | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining | ||
7 | * a copy of this software and associated documentation files (the | ||
8 | * ``Software''), to deal in the Software without restriction, including | ||
9 | * without limitation the rights to use, copy, modify, merge, publish, | ||
10 | * distribute, sublicense, and/or sell copies of the Software, and to | ||
11 | * permit persons to whom the Software is furnished to do so, subject to | ||
12 | * the following conditions: | ||
13 | * | ||
14 | * The above copyright notice and this permission notice shall be included | ||
15 | * in all copies or substantial portions of the Software. | ||
16 | * | ||
17 | * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
20 | * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
23 | * OTHER DEALINGS IN THE SOFTWARE. | ||
24 | * | ||
25 | * $Id: dirent.h,v 1.20 2014/03/19 17:52:23 tronkko Exp $ | ||
26 | */ | ||
27 | #ifndef DIRENT_MSVC_H | ||
28 | #define DIRENT_MSVC_H | ||
29 | |||
30 | #include <windows.h> | ||
31 | |||
32 | #include <../include/stdio.h> | ||
33 | #include <../include/stdarg.h> | ||
34 | #include <../include/wchar.h> | ||
35 | #include <../include/string.h> | ||
36 | #include <../include/stdlib.h> | ||
37 | #include <../include/malloc.h> | ||
38 | #include <../include/sys/types.h> | ||
39 | #include <sys/stat.h> | ||
40 | #include <../include/errno.h> | ||
41 | |||
42 | /* Indicates that d_type field is available in dirent structure */ | ||
43 | #define _DIRENT_HAVE_D_TYPE | ||
44 | |||
45 | /* Indicates that d_namlen field is available in dirent structure */ | ||
46 | #define _DIRENT_HAVE_D_NAMLEN | ||
47 | |||
48 | /* Entries missing from MSVC 6.0 */ | ||
49 | #if !defined(FILE_ATTRIBUTE_DEVICE) | ||
50 | # define FILE_ATTRIBUTE_DEVICE 0x40 | ||
51 | #endif | ||
52 | |||
53 | /* Maximum length of file name */ | ||
54 | #if !defined(PATH_MAX) | ||
55 | # define PATH_MAX MAX_PATH | ||
56 | #endif | ||
57 | #if !defined(FILENAME_MAX) | ||
58 | # define FILENAME_MAX MAX_PATH | ||
59 | #endif | ||
60 | #if !defined(NAME_MAX) | ||
61 | # define NAME_MAX FILENAME_MAX | ||
62 | #endif | ||
63 | |||
64 | /* Return the exact length of d_namlen without zero terminator */ | ||
65 | #define _D_EXACT_NAMLEN(p) ((p)->d_namlen) | ||
66 | |||
67 | /* Return number of bytes needed to store d_namlen */ | ||
68 | #define _D_ALLOC_NAMLEN(p) (PATH_MAX) | ||
69 | |||
70 | |||
71 | #ifdef __cplusplus | ||
72 | extern "C" { | ||
73 | #endif | ||
74 | |||
75 | |||
76 | /* Wide-character version */ | ||
77 | struct _wdirent { | ||
78 | long d_ino; /* Always zero */ | ||
79 | unsigned short d_reclen; /* Structure size */ | ||
80 | size_t d_namlen; /* Length of name without \0 */ | ||
81 | int d_type; /* File type */ | ||
82 | wchar_t d_name[PATH_MAX]; /* File name */ | ||
83 | }; | ||
84 | typedef struct _wdirent _wdirent; | ||
85 | |||
86 | struct _WDIR { | ||
87 | struct _wdirent ent; /* Current directory entry */ | ||
88 | WIN32_FIND_DATAW data; /* Private file data */ | ||
89 | int cached; /* True if data is valid */ | ||
90 | HANDLE handle; /* Win32 search handle */ | ||
91 | wchar_t *patt; /* Initial directory name */ | ||
92 | }; | ||
93 | typedef struct _WDIR _WDIR; | ||
94 | |||
95 | static _WDIR *_wopendir (const wchar_t *dirname); | ||
96 | static struct _wdirent *_wreaddir (_WDIR *dirp); | ||
97 | static int _wclosedir (_WDIR *dirp); | ||
98 | static void _wrewinddir (_WDIR* dirp); | ||
99 | |||
100 | |||
101 | /* For compatibility with Symbian */ | ||
102 | #define wdirent _wdirent | ||
103 | #define WDIR _WDIR | ||
104 | #define wopendir _wopendir | ||
105 | #define wreaddir _wreaddir | ||
106 | #define wclosedir _wclosedir | ||
107 | #define wrewinddir _wrewinddir | ||
108 | |||
109 | |||
110 | /* Multi-byte character versions */ | ||
111 | struct dirent { | ||
112 | long d_ino; /* Always zero */ | ||
113 | unsigned short d_reclen; /* Structure size */ | ||
114 | size_t d_namlen; /* Length of name without \0 */ | ||
115 | int d_type; /* File type */ | ||
116 | char d_name[PATH_MAX]; /* File name */ | ||
117 | }; | ||
118 | typedef struct dirent dirent; | ||
119 | |||
120 | struct DIR { | ||
121 | struct dirent ent; | ||
122 | struct _WDIR *wdirp; | ||
123 | }; | ||
124 | typedef struct DIR DIR; | ||
125 | |||
126 | static DIR *opendir (const char *dirname); | ||
127 | static struct dirent *readdir (DIR *dirp); | ||
128 | static int closedir (DIR *dirp); | ||
129 | static void rewinddir (DIR* dirp); | ||
130 | |||
131 | |||
132 | /* Internal utility functions */ | ||
133 | static WIN32_FIND_DATAW *dirent_first (_WDIR *dirp); | ||
134 | static WIN32_FIND_DATAW *dirent_next (_WDIR *dirp); | ||
135 | |||
136 | static int dirent_mbstowcs_s( | ||
137 | size_t *pReturnValue, | ||
138 | wchar_t *wcstr, | ||
139 | size_t sizeInWords, | ||
140 | const char *mbstr, | ||
141 | size_t count); | ||
142 | |||
143 | static int dirent_wcstombs_s( | ||
144 | size_t *pReturnValue, | ||
145 | char *mbstr, | ||
146 | size_t sizeInBytes, | ||
147 | const wchar_t *wcstr, | ||
148 | size_t count); | ||
149 | |||
150 | static void dirent_set_errno (int error); | ||
151 | |||
152 | /* | ||
153 | * Open directory stream DIRNAME for read and return a pointer to the | ||
154 | * internal working area that is used to retrieve individual directory | ||
155 | * entries. | ||
156 | */ | ||
157 | static _WDIR* | ||
158 | _wopendir( | ||
159 | const wchar_t *dirname) | ||
160 | { | ||
161 | _WDIR *dirp = NULL; | ||
162 | int error; | ||
163 | |||
164 | /* Must have directory name */ | ||
165 | if (dirname == NULL || dirname[0] == '\0') { | ||
166 | dirent_set_errno (ENOENT); | ||
167 | return NULL; | ||
168 | } | ||
169 | |||
170 | /* Allocate new _WDIR structure */ | ||
171 | dirp = (_WDIR*) malloc (sizeof (struct _WDIR)); | ||
172 | if (dirp != NULL) { | ||
173 | DWORD n; | ||
174 | |||
175 | /* Reset _WDIR structure */ | ||
176 | dirp->handle = INVALID_HANDLE_VALUE; | ||
177 | dirp->patt = NULL; | ||
178 | dirp->cached = 0; | ||
179 | |||
180 | /* Compute the length of full path plus zero terminator */ | ||
181 | n = GetFullPathNameW (dirname, 0, NULL, NULL); | ||
182 | |||
183 | /* Allocate room for absolute directory name and search pattern */ | ||
184 | dirp->patt = (wchar_t*) malloc (sizeof (wchar_t) * n + 16); | ||
185 | if (dirp->patt) { | ||
186 | |||
187 | /* | ||
188 | * Convert relative directory name to an absolute one. This | ||
189 | * allows rewinddir() to function correctly even when current | ||
190 | * working directory is changed between opendir() and rewinddir(). | ||
191 | */ | ||
192 | n = GetFullPathNameW (dirname, n, dirp->patt, NULL); | ||
193 | if (n > 0) { | ||
194 | wchar_t *p; | ||
195 | |||
196 | /* Append search pattern \* to the directory name */ | ||
197 | p = dirp->patt + n; | ||
198 | if (dirp->patt < p) { | ||
199 | switch (p[-1]) { | ||
200 | case '\\': | ||
201 | case '/': | ||
202 | case ':': | ||
203 | /* Directory ends in path separator, e.g. c:\temp\ */ | ||
204 | /*NOP*/; | ||
205 | break; | ||
206 | |||
207 | default: | ||
208 | /* Directory name doesn't end in path separator */ | ||
209 | *p++ = '\\'; | ||
210 | } | ||
211 | } | ||
212 | *p++ = '*'; | ||
213 | *p = '\0'; | ||
214 | |||
215 | /* Open directory stream and retrieve the first entry */ | ||
216 | if (dirent_first (dirp)) { | ||
217 | /* Directory stream opened successfully */ | ||
218 | error = 0; | ||
219 | } else { | ||
220 | /* Cannot retrieve first entry */ | ||
221 | error = 1; | ||
222 | dirent_set_errno (ENOENT); | ||
223 | } | ||
224 | |||
225 | } else { | ||
226 | /* Cannot retrieve full path name */ | ||
227 | dirent_set_errno (ENOENT); | ||
228 | error = 1; | ||
229 | } | ||
230 | |||
231 | } else { | ||
232 | /* Cannot allocate memory for search pattern */ | ||
233 | error = 1; | ||
234 | } | ||
235 | |||
236 | } else { | ||
237 | /* Cannot allocate _WDIR structure */ | ||
238 | error = 1; | ||
239 | } | ||
240 | |||
241 | /* Clean up in case of error */ | ||
242 | if (error && dirp) { | ||
243 | _wclosedir (dirp); | ||
244 | dirp = NULL; | ||
245 | } | ||
246 | |||
247 | return dirp; | ||
248 | } | ||
249 | |||
250 | /* | ||
251 | * Read next directory entry. The directory entry is returned in dirent | ||
252 | * structure in the d_name field. Individual directory entries returned by | ||
253 | * this function include regular files, sub-directories, pseudo-directories | ||
254 | * "." and ".." as well as volume labels, hidden files and system files. | ||
255 | */ | ||
256 | static struct _wdirent* | ||
257 | _wreaddir( | ||
258 | _WDIR *dirp) | ||
259 | { | ||
260 | WIN32_FIND_DATAW *datap; | ||
261 | struct _wdirent *entp; | ||
262 | |||
263 | /* Read next directory entry */ | ||
264 | datap = dirent_next (dirp); | ||
265 | if (datap) { | ||
266 | size_t n; | ||
267 | DWORD attr; | ||
268 | |||
269 | /* Pointer to directory entry to return */ | ||
270 | entp = &dirp->ent; | ||
271 | |||
272 | /* | ||
273 | * Copy file name as wide-character string. If the file name is too | ||
274 | * long to fit in to the destination buffer, then truncate file name | ||
275 | * to PATH_MAX characters and zero-terminate the buffer. | ||
276 | */ | ||
277 | n = 0; | ||
278 | while (n + 1 < PATH_MAX && datap->cFileName[n] != 0) { | ||
279 | entp->d_name[n] = datap->cFileName[n]; | ||
280 | n++; | ||
281 | } | ||
282 | dirp->ent.d_name[n] = 0; | ||
283 | |||
284 | /* Length of file name excluding zero terminator */ | ||
285 | entp->d_namlen = n; | ||
286 | |||
287 | /* File type */ | ||
288 | attr = datap->dwFileAttributes; | ||
289 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) { | ||
290 | entp->d_type = DT_CHR; | ||
291 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | ||
292 | entp->d_type = DT_DIR; | ||
293 | } else { | ||
294 | entp->d_type = DT_REG; | ||
295 | } | ||
296 | |||
297 | /* Reset dummy fields */ | ||
298 | entp->d_ino = 0; | ||
299 | entp->d_reclen = sizeof (struct _wdirent); | ||
300 | |||
301 | } else { | ||
302 | |||
303 | /* Last directory entry read */ | ||
304 | entp = NULL; | ||
305 | |||
306 | } | ||
307 | |||
308 | return entp; | ||
309 | } | ||
310 | |||
311 | /* | ||
312 | * Close directory stream opened by opendir() function. This invalidates the | ||
313 | * DIR structure as well as any directory entry read previously by | ||
314 | * _wreaddir(). | ||
315 | */ | ||
316 | static int | ||
317 | _wclosedir( | ||
318 | _WDIR *dirp) | ||
319 | { | ||
320 | int ok; | ||
321 | if (dirp) { | ||
322 | |||
323 | /* Release search handle */ | ||
324 | if (dirp->handle != INVALID_HANDLE_VALUE) { | ||
325 | FindClose (dirp->handle); | ||
326 | dirp->handle = INVALID_HANDLE_VALUE; | ||
327 | } | ||
328 | |||
329 | /* Release search pattern */ | ||
330 | if (dirp->patt) { | ||
331 | free (dirp->patt); | ||
332 | dirp->patt = NULL; | ||
333 | } | ||
334 | |||
335 | /* Release directory structure */ | ||
336 | free (dirp); | ||
337 | ok = /*success*/0; | ||
338 | |||
339 | } else { | ||
340 | /* Invalid directory stream */ | ||
341 | dirent_set_errno (EBADF); | ||
342 | ok = /*failure*/-1; | ||
343 | } | ||
344 | return ok; | ||
345 | } | ||
346 | |||
347 | /* | ||
348 | * Rewind directory stream such that _wreaddir() returns the very first | ||
349 | * file name again. | ||
350 | */ | ||
351 | static void | ||
352 | _wrewinddir( | ||
353 | _WDIR* dirp) | ||
354 | { | ||
355 | if (dirp) { | ||
356 | /* Release existing search handle */ | ||
357 | if (dirp->handle != INVALID_HANDLE_VALUE) { | ||
358 | FindClose (dirp->handle); | ||
359 | } | ||
360 | |||
361 | /* Open new search handle */ | ||
362 | dirent_first (dirp); | ||
363 | } | ||
364 | } | ||
365 | |||
366 | /* Get first directory entry (internal) */ | ||
367 | static WIN32_FIND_DATAW* | ||
368 | dirent_first( | ||
369 | _WDIR *dirp) | ||
370 | { | ||
371 | WIN32_FIND_DATAW *datap; | ||
372 | |||
373 | /* Open directory and retrieve the first entry */ | ||
374 | dirp->handle = FindFirstFileW (dirp->patt, &dirp->data); | ||
375 | if (dirp->handle != INVALID_HANDLE_VALUE) { | ||
376 | |||
377 | /* a directory entry is now waiting in memory */ | ||
378 | datap = &dirp->data; | ||
379 | dirp->cached = 1; | ||
380 | |||
381 | } else { | ||
382 | |||
383 | /* Failed to re-open directory: no directory entry in memory */ | ||
384 | dirp->cached = 0; | ||
385 | datap = NULL; | ||
386 | |||
387 | } | ||
388 | return datap; | ||
389 | } | ||
390 | |||
391 | /* Get next directory entry (internal) */ | ||
392 | static WIN32_FIND_DATAW* | ||
393 | dirent_next( | ||
394 | _WDIR *dirp) | ||
395 | { | ||
396 | WIN32_FIND_DATAW *p; | ||
397 | |||
398 | /* Get next directory entry */ | ||
399 | if (dirp->cached != 0) { | ||
400 | |||
401 | /* A valid directory entry already in memory */ | ||
402 | p = &dirp->data; | ||
403 | dirp->cached = 0; | ||
404 | |||
405 | } else if (dirp->handle != INVALID_HANDLE_VALUE) { | ||
406 | |||
407 | /* Get the next directory entry from stream */ | ||
408 | if (FindNextFileW (dirp->handle, &dirp->data) != FALSE) { | ||
409 | /* Got a file */ | ||
410 | p = &dirp->data; | ||
411 | } else { | ||
412 | /* The very last entry has been processed or an error occured */ | ||
413 | FindClose (dirp->handle); | ||
414 | dirp->handle = INVALID_HANDLE_VALUE; | ||
415 | p = NULL; | ||
416 | } | ||
417 | |||
418 | } else { | ||
419 | |||
420 | /* End of directory stream reached */ | ||
421 | p = NULL; | ||
422 | |||
423 | } | ||
424 | |||
425 | return p; | ||
426 | } | ||
427 | |||
428 | /* | ||
429 | * Open directory stream using plain old C-string. | ||
430 | */ | ||
431 | static DIR* | ||
432 | opendir( | ||
433 | const char *dirname) | ||
434 | { | ||
435 | struct DIR *dirp; | ||
436 | int error; | ||
437 | |||
438 | /* Must have directory name */ | ||
439 | if (dirname == NULL || dirname[0] == '\0') { | ||
440 | dirent_set_errno (ENOENT); | ||
441 | return NULL; | ||
442 | } | ||
443 | |||
444 | /* Allocate memory for DIR structure */ | ||
445 | dirp = (DIR*) malloc (sizeof (struct DIR)); | ||
446 | if (dirp) { | ||
447 | wchar_t wname[PATH_MAX]; | ||
448 | size_t n; | ||
449 | |||
450 | /* Convert directory name to wide-character string */ | ||
451 | error = dirent_mbstowcs_s (&n, wname, PATH_MAX, dirname, PATH_MAX); | ||
452 | if (!error) { | ||
453 | |||
454 | /* Open directory stream using wide-character name */ | ||
455 | dirp->wdirp = _wopendir (wname); | ||
456 | if (dirp->wdirp) { | ||
457 | /* Directory stream opened */ | ||
458 | error = 0; | ||
459 | } else { | ||
460 | /* Failed to open directory stream */ | ||
461 | error = 1; | ||
462 | } | ||
463 | |||
464 | } else { | ||
465 | /* | ||
466 | * Cannot convert file name to wide-character string. This | ||
467 | * occurs if the string contains invalid multi-byte sequences or | ||
468 | * the output buffer is too small to contain the resulting | ||
469 | * string. | ||
470 | */ | ||
471 | error = 1; | ||
472 | } | ||
473 | |||
474 | } else { | ||
475 | /* Cannot allocate DIR structure */ | ||
476 | error = 1; | ||
477 | } | ||
478 | |||
479 | /* Clean up in case of error */ | ||
480 | if (error && dirp) { | ||
481 | free (dirp); | ||
482 | dirp = NULL; | ||
483 | } | ||
484 | |||
485 | return dirp; | ||
486 | } | ||
487 | |||
488 | /* | ||
489 | * Read next directory entry. | ||
490 | * | ||
491 | * When working with text consoles, please note that file names returned by | ||
492 | * readdir() are represented in the default ANSI code page while any output to | ||
493 | * console is typically formatted on another code page. Thus, non-ASCII | ||
494 | * characters in file names will not usually display correctly on console. The | ||
495 | * problem can be fixed in two ways: (1) change the character set of console | ||
496 | * to 1252 using chcp utility and use Lucida Console font, or (2) use | ||
497 | * _cprintf function when writing to console. The _cprinf() will re-encode | ||
498 | * ANSI strings to the console code page so many non-ASCII characters will | ||
499 | * display correcly. | ||
500 | */ | ||
501 | static struct dirent* | ||
502 | readdir( | ||
503 | DIR *dirp) | ||
504 | { | ||
505 | WIN32_FIND_DATAW *datap; | ||
506 | struct dirent *entp; | ||
507 | |||
508 | /* Read next directory entry */ | ||
509 | datap = dirent_next (dirp->wdirp); | ||
510 | if (datap) { | ||
511 | size_t n; | ||
512 | int error; | ||
513 | |||
514 | /* Attempt to convert file name to multi-byte string */ | ||
515 | error = dirent_wcstombs_s( | ||
516 | &n, dirp->ent.d_name, PATH_MAX, datap->cFileName, PATH_MAX); | ||
517 | |||
518 | /* | ||
519 | * If the file name cannot be represented by a multi-byte string, | ||
520 | * then attempt to use old 8+3 file name. This allows traditional | ||
521 | * Unix-code to access some file names despite of unicode | ||
522 | * characters, although file names may seem unfamiliar to the user. | ||
523 | * | ||
524 | * Be ware that the code below cannot come up with a short file | ||
525 | * name unless the file system provides one. At least | ||
526 | * VirtualBox shared folders fail to do this. | ||
527 | */ | ||
528 | if (error && datap->cAlternateFileName[0] != '\0') { | ||
529 | error = dirent_wcstombs_s( | ||
530 | &n, dirp->ent.d_name, PATH_MAX, | ||
531 | datap->cAlternateFileName, PATH_MAX); | ||
532 | } | ||
533 | |||
534 | if (!error) { | ||
535 | DWORD attr; | ||
536 | |||
537 | /* Initialize directory entry for return */ | ||
538 | entp = &dirp->ent; | ||
539 | |||
540 | /* Length of file name excluding zero terminator */ | ||
541 | entp->d_namlen = n - 1; | ||
542 | |||
543 | /* File attributes */ | ||
544 | attr = datap->dwFileAttributes; | ||
545 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) { | ||
546 | entp->d_type = DT_CHR; | ||
547 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | ||
548 | entp->d_type = DT_DIR; | ||
549 | } else { | ||
550 | entp->d_type = DT_REG; | ||
551 | } | ||
552 | |||
553 | /* Reset dummy fields */ | ||
554 | entp->d_ino = 0; | ||
555 | entp->d_reclen = sizeof (struct dirent); | ||
556 | |||
557 | } else { | ||
558 | /* | ||
559 | * Cannot convert file name to multi-byte string so construct | ||
560 | * an errornous directory entry and return that. Note that | ||
561 | * we cannot return NULL as that would stop the processing | ||
562 | * of directory entries completely. | ||
563 | */ | ||
564 | entp = &dirp->ent; | ||
565 | entp->d_name[0] = '?'; | ||
566 | entp->d_name[1] = '\0'; | ||
567 | entp->d_namlen = 1; | ||
568 | entp->d_type = DT_UNKNOWN; | ||
569 | entp->d_ino = 0; | ||
570 | entp->d_reclen = 0; | ||
571 | } | ||
572 | |||
573 | } else { | ||
574 | /* No more directory entries */ | ||
575 | entp = NULL; | ||
576 | } | ||
577 | |||
578 | return entp; | ||
579 | } | ||
580 | |||
581 | /* | ||
582 | * Close directory stream. | ||
583 | */ | ||
584 | static int | ||
585 | closedir( | ||
586 | DIR *dirp) | ||
587 | { | ||
588 | int ok; | ||
589 | if (dirp) { | ||
590 | |||
591 | /* Close wide-character directory stream */ | ||
592 | ok = _wclosedir (dirp->wdirp); | ||
593 | dirp->wdirp = NULL; | ||
594 | |||
595 | /* Release multi-byte character version */ | ||
596 | free (dirp); | ||
597 | |||
598 | } else { | ||
599 | |||
600 | /* Invalid directory stream */ | ||
601 | dirent_set_errno (EBADF); | ||
602 | ok = /*failure*/-1; | ||
603 | |||
604 | } | ||
605 | return ok; | ||
606 | } | ||
607 | |||
608 | /* | ||
609 | * Rewind directory stream to beginning. | ||
610 | */ | ||
611 | static void | ||
612 | rewinddir( | ||
613 | DIR* dirp) | ||
614 | { | ||
615 | /* Rewind wide-character string directory stream */ | ||
616 | _wrewinddir (dirp->wdirp); | ||
617 | } | ||
618 | |||
619 | /* Convert multi-byte string to wide character string */ | ||
620 | static int | ||
621 | dirent_mbstowcs_s( | ||
622 | size_t *pReturnValue, | ||
623 | wchar_t *wcstr, | ||
624 | size_t sizeInWords, | ||
625 | const char *mbstr, | ||
626 | size_t count) | ||
627 | { | ||
628 | int error; | ||
629 | |||
630 | #if defined(_MSC_VER) && _MSC_VER >= 1400 | ||
631 | |||
632 | /* Microsoft Visual Studio 2005 or later */ | ||
633 | error = mbstowcs_s (pReturnValue, wcstr, sizeInWords, mbstr, count); | ||
634 | |||
635 | #else | ||
636 | |||
637 | /* Older Visual Studio or non-Microsoft compiler */ | ||
638 | size_t n; | ||
639 | |||
640 | /* Convert to wide-character string (or count characters) */ | ||
641 | n = mbstowcs (wcstr, mbstr, sizeInWords); | ||
642 | if (!wcstr || n < count) { | ||
643 | |||
644 | /* Zero-terminate output buffer */ | ||
645 | if (wcstr && sizeInWords) { | ||
646 | if (n >= sizeInWords) { | ||
647 | n = sizeInWords - 1; | ||
648 | } | ||
649 | wcstr[n] = 0; | ||
650 | } | ||
651 | |||
652 | /* Length of resuting multi-byte string WITH zero terminator */ | ||
653 | if (pReturnValue) { | ||
654 | *pReturnValue = n + 1; | ||
655 | } | ||
656 | |||
657 | /* Success */ | ||
658 | error = 0; | ||
659 | |||
660 | } else { | ||
661 | |||
662 | /* Could not convert string */ | ||
663 | error = 1; | ||
664 | |||
665 | } | ||
666 | |||
667 | #endif | ||
668 | |||
669 | return error; | ||
670 | } | ||
671 | |||
672 | /* Convert wide-character string to multi-byte string */ | ||
673 | static int | ||
674 | dirent_wcstombs_s( | ||
675 | size_t *pReturnValue, | ||
676 | char *mbstr, | ||
677 | size_t sizeInBytes, /* max size of mbstr */ | ||
678 | const wchar_t *wcstr, | ||
679 | size_t count) | ||
680 | { | ||
681 | int error; | ||
682 | |||
683 | #if defined(_MSC_VER) && _MSC_VER >= 1400 | ||
684 | |||
685 | /* Microsoft Visual Studio 2005 or later */ | ||
686 | error = wcstombs_s (pReturnValue, mbstr, sizeInBytes, wcstr, count); | ||
687 | |||
688 | #else | ||
689 | |||
690 | /* Older Visual Studio or non-Microsoft compiler */ | ||
691 | size_t n; | ||
692 | |||
693 | /* Convert to multi-byte string (or count the number of bytes needed) */ | ||
694 | n = wcstombs (mbstr, wcstr, sizeInBytes); | ||
695 | if (!mbstr || n < count) { | ||
696 | |||
697 | /* Zero-terminate output buffer */ | ||
698 | if (mbstr && sizeInBytes) { | ||
699 | if (n >= sizeInBytes) { | ||
700 | n = sizeInBytes - 1; | ||
701 | } | ||
702 | mbstr[n] = '\0'; | ||
703 | } | ||
704 | |||
705 | /* Lenght of resulting multi-bytes string WITH zero-terminator */ | ||
706 | if (pReturnValue) { | ||
707 | *pReturnValue = n + 1; | ||
708 | } | ||
709 | |||
710 | /* Success */ | ||
711 | error = 0; | ||
712 | |||
713 | } else { | ||
714 | |||
715 | /* Cannot convert string */ | ||
716 | error = 1; | ||
717 | |||
718 | } | ||
719 | |||
720 | #endif | ||
721 | |||
722 | return error; | ||
723 | } | ||
724 | |||
725 | /* Set errno variable */ | ||
726 | static void | ||
727 | dirent_set_errno( | ||
728 | int error) | ||
729 | { | ||
730 | #if defined(_MSC_VER) && _MSC_VER >= 1400 | ||
731 | |||
732 | /* Microsoft Visual Studio 2005 and later */ | ||
733 | _set_errno (error); | ||
734 | |||
735 | #else | ||
736 | |||
737 | /* Non-Microsoft compiler or older Microsoft compiler */ | ||
738 | errno = error; | ||
739 | |||
740 | #endif | ||
741 | } | ||
742 | |||
743 | |||
744 | #ifdef __cplusplus | ||
745 | } | ||
746 | #endif | ||
747 | #endif /*DIRENT_H*/ | ||
748 | |||
diff --git a/include/compat/err.h b/include/compat/err.h new file mode 100644 index 0000000..af68a26 --- /dev/null +++ b/include/compat/err.h | |||
@@ -0,0 +1,33 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * err.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifdef HAVE_ERR_H | ||
7 | |||
8 | #include_next <err.h> | ||
9 | |||
10 | #else | ||
11 | |||
12 | #ifndef LIBCRYPTOCOMPAT_ERR_H | ||
13 | #define LIBCRYPTOCOMPAT_ERR_H | ||
14 | |||
15 | #include <errno.h> | ||
16 | #include <stdio.h> | ||
17 | #include <string.h> | ||
18 | |||
19 | #define err(exitcode, format, ...) \ | ||
20 | errx(exitcode, format ": %s", __VA_ARGS__, strerror(errno)) | ||
21 | |||
22 | #define errx(exitcode, format, ...) \ | ||
23 | do { warnx(format, __VA_ARGS__); exit(exitcode); } while (0) | ||
24 | |||
25 | #define warn(format, ...) \ | ||
26 | warnx(format ": %s", __VA_ARGS__, strerror(errno)) | ||
27 | |||
28 | #define warnx(format, ...) \ | ||
29 | fprintf(stderr, format "\n", __VA_ARGS__) | ||
30 | |||
31 | #endif | ||
32 | |||
33 | #endif | ||
diff --git a/include/compat/machine/endian.h b/include/compat/machine/endian.h new file mode 100644 index 0000000..5ec39af --- /dev/null +++ b/include/compat/machine/endian.h | |||
@@ -0,0 +1,40 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * machine/endian.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_BYTE_ORDER_H_ | ||
7 | #define LIBCRYPTOCOMPAT_BYTE_ORDER_H_ | ||
8 | |||
9 | #if defined(_WIN32) | ||
10 | |||
11 | #define LITTLE_ENDIAN 1234 | ||
12 | #define BIG_ENDIAN 4321 | ||
13 | #define PDP_ENDIAN 3412 | ||
14 | |||
15 | /* | ||
16 | * Use GCC and Visual Studio compiler defines to determine endian. | ||
17 | */ | ||
18 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | ||
19 | #define BYTE_ORDER LITTLE_ENDIAN | ||
20 | #else | ||
21 | #define BYTE_ORDER BIG_ENDIAN | ||
22 | #endif | ||
23 | |||
24 | #elif defined(__linux__) | ||
25 | #include <endian.h> | ||
26 | |||
27 | #elif defined(__sun) || defined(_AIX) || defined(__hpux) | ||
28 | #include <sys/types.h> | ||
29 | #include <arpa/nameser_compat.h> | ||
30 | |||
31 | #elif defined(__sgi) | ||
32 | #include <standards.h> | ||
33 | #include <sys/endian.h> | ||
34 | |||
35 | #else | ||
36 | #include_next <machine/endian.h> | ||
37 | |||
38 | #endif | ||
39 | |||
40 | #endif | ||
diff --git a/include/compat/netdb.h b/include/compat/netdb.h new file mode 100644 index 0000000..d36b91d --- /dev/null +++ b/include/compat/netdb.h | |||
@@ -0,0 +1,10 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * netdb.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <netdb.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | #endif | ||
diff --git a/include/compat/netinet/in.h b/include/compat/netinet/in.h new file mode 100644 index 0000000..20e57b8 --- /dev/null +++ b/include/compat/netinet/in.h | |||
@@ -0,0 +1,10 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * netinet/in.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <netinet/in.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | #endif | ||
diff --git a/include/compat/netinet/tcp.h b/include/compat/netinet/tcp.h new file mode 100644 index 0000000..c98cf74 --- /dev/null +++ b/include/compat/netinet/tcp.h | |||
@@ -0,0 +1,10 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * netinet/tcp.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <netinet/tcp.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | #endif | ||
diff --git a/include/compat/poll.h b/include/compat/poll.h new file mode 100644 index 0000000..e9204cf --- /dev/null +++ b/include/compat/poll.h | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * | ||
4 | * poll(2) emulation for Windows | ||
5 | * | ||
6 | * This emulates just-enough poll functionality on Windows to work in the | ||
7 | * context of the openssl(1) program. This is not a replacement for | ||
8 | * POSIX.1-2001 poll(2). | ||
9 | * | ||
10 | * Dongsheng Song <dongsheng.song@gmail.com> | ||
11 | * Brent Cook <bcook@openbsd.org> | ||
12 | */ | ||
13 | |||
14 | #ifndef LIBCRYPTOCOMPAT_POLL_H | ||
15 | #define LIBCRYPTOCOMPAT_POLL_H | ||
16 | |||
17 | #ifndef _WIN32 | ||
18 | #include_next <poll.h> | ||
19 | #else | ||
20 | |||
21 | #include <winsock2.h> | ||
22 | |||
23 | /* Type used for the number of file descriptors. */ | ||
24 | typedef unsigned long int nfds_t; | ||
25 | |||
26 | #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600) | ||
27 | /* Data structure describing a polling request. */ | ||
28 | struct pollfd { | ||
29 | int fd; /* file descriptor */ | ||
30 | short events; /* requested events */ | ||
31 | short revents; /* returned events */ | ||
32 | }; | ||
33 | |||
34 | /* Event types that can be polled */ | ||
35 | #define POLLIN 0x001 /* There is data to read. */ | ||
36 | #define POLLPRI 0x002 /* There is urgent data to read. */ | ||
37 | #define POLLOUT 0x004 /* Writing now will not block. */ | ||
38 | |||
39 | # define POLLRDNORM 0x040 /* Normal data may be read. */ | ||
40 | # define POLLRDBAND 0x080 /* Priority data may be read. */ | ||
41 | # define POLLWRNORM 0x100 /* Writing now will not block. */ | ||
42 | # define POLLWRBAND 0x200 /* Priority data may be written. */ | ||
43 | |||
44 | /* Event types always implicitly polled. */ | ||
45 | #define POLLERR 0x008 /* Error condition. */ | ||
46 | #define POLLHUP 0x010 /* Hung up. */ | ||
47 | #define POLLNVAL 0x020 /* Invalid polling request. */ | ||
48 | |||
49 | #endif | ||
50 | |||
51 | #ifdef __cplusplus | ||
52 | extern "C" { | ||
53 | #endif | ||
54 | |||
55 | int poll(struct pollfd *pfds, nfds_t nfds, int timeout); | ||
56 | |||
57 | #ifdef __cplusplus | ||
58 | } | ||
59 | #endif | ||
60 | |||
61 | #endif /* HAVE_POLL */ | ||
62 | |||
63 | #endif /* LIBCRYPTOCOMPAT_POLL_H */ | ||
diff --git a/include/compat/stdio.h b/include/compat/stdio.h new file mode 100644 index 0000000..973faa4 --- /dev/null +++ b/include/compat/stdio.h | |||
@@ -0,0 +1,39 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * stdio.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_STDIO_H | ||
7 | #define LIBCRYPTOCOMPAT_STDIO_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <../include/stdio.h> | ||
11 | #else | ||
12 | #include_next <stdio.h> | ||
13 | #endif | ||
14 | |||
15 | #ifndef HAVE_ASPRINTF | ||
16 | #include <stdarg.h> | ||
17 | int vasprintf(char **str, const char *fmt, va_list ap); | ||
18 | int asprintf(char **str, const char *fmt, ...); | ||
19 | #endif | ||
20 | |||
21 | #ifdef _WIN32 | ||
22 | |||
23 | void posix_perror(const char *s); | ||
24 | FILE * posix_fopen(const char *path, const char *mode); | ||
25 | int posix_rename(const char *oldpath, const char *newpath); | ||
26 | |||
27 | #ifndef NO_REDEF_POSIX_FUNCTIONS | ||
28 | #define perror(errnum) posix_perror(errnum) | ||
29 | #define fopen(path, mode) posix_fopen(path, mode) | ||
30 | #define rename(oldpath, newpath) posix_rename(oldpath, newpath) | ||
31 | #endif | ||
32 | |||
33 | #ifdef _MSC_VER | ||
34 | #define snprintf _snprintf | ||
35 | #endif | ||
36 | |||
37 | #endif | ||
38 | |||
39 | #endif | ||
diff --git a/include/compat/stdlib.h b/include/compat/stdlib.h new file mode 100644 index 0000000..47189fd --- /dev/null +++ b/include/compat/stdlib.h | |||
@@ -0,0 +1,34 @@ | |||
1 | /* | ||
2 | * stdlib.h compatibility shim | ||
3 | * Public domain | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_STDLIB_H | ||
7 | #define LIBCRYPTOCOMPAT_STDLIB_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <../include/stdlib.h> | ||
11 | #else | ||
12 | #include_next <stdlib.h> | ||
13 | #endif | ||
14 | |||
15 | #include <sys/types.h> | ||
16 | #include <sys/stat.h> | ||
17 | //#include <sys/time.h> | ||
18 | #include <stdint.h> | ||
19 | |||
20 | #ifndef HAVE_ARC4RANDOM_BUF | ||
21 | uint32_t arc4random(void); | ||
22 | void arc4random_buf(void *_buf, size_t n); | ||
23 | #endif | ||
24 | |||
25 | #ifndef HAVE_REALLOCARRAY | ||
26 | void *reallocarray(void *, size_t, size_t); | ||
27 | #endif | ||
28 | |||
29 | #ifndef HAVE_STRTONUM | ||
30 | long long strtonum(const char *nptr, long long minval, | ||
31 | long long maxval, const char **errstr); | ||
32 | #endif | ||
33 | |||
34 | #endif | ||
diff --git a/include/compat/string.h b/include/compat/string.h new file mode 100644 index 0000000..eabc4c4 --- /dev/null +++ b/include/compat/string.h | |||
@@ -0,0 +1,82 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * string.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_STRING_H | ||
7 | #define LIBCRYPTOCOMPAT_STRING_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <../include/string.h> | ||
11 | #else | ||
12 | #include_next <string.h> | ||
13 | #endif | ||
14 | |||
15 | #include <sys/types.h> | ||
16 | |||
17 | #if defined(__sun) || defined(__hpux) | ||
18 | /* Some functions historically defined in string.h were placed in strings.h by | ||
19 | * SUS. Use the same hack as OS X and FreeBSD use to work around on Solaris and HPUX. | ||
20 | */ | ||
21 | #include <strings.h> | ||
22 | #endif | ||
23 | |||
24 | #ifndef HAVE_STRCASECMP | ||
25 | int strcasecmp(const char *s1, const char *s2); | ||
26 | int strncasecmp(const char *s1, const char *s2, size_t len); | ||
27 | #endif | ||
28 | |||
29 | #ifndef HAVE_STRLCPY | ||
30 | size_t strlcpy(char *dst, const char *src, size_t siz); | ||
31 | #endif | ||
32 | |||
33 | #ifndef HAVE_STRLCAT | ||
34 | size_t strlcat(char *dst, const char *src, size_t siz); | ||
35 | #endif | ||
36 | |||
37 | #ifndef HAVE_STRNDUP | ||
38 | char * strndup(const char *str, size_t maxlen); | ||
39 | /* the only user of strnlen is strndup, so only build it if needed */ | ||
40 | #ifndef HAVE_STRNLEN | ||
41 | size_t strnlen(const char *str, size_t maxlen); | ||
42 | #endif | ||
43 | #endif | ||
44 | |||
45 | #ifndef HAVE_STRSEP | ||
46 | char *strsep(char **stringp, const char *delim); | ||
47 | #endif | ||
48 | |||
49 | #ifndef HAVE_EXPLICIT_BZERO | ||
50 | void explicit_bzero(void *, size_t); | ||
51 | #endif | ||
52 | |||
53 | #ifndef HAVE_TIMINGSAFE_BCMP | ||
54 | int timingsafe_bcmp(const void *b1, const void *b2, size_t n); | ||
55 | #endif | ||
56 | |||
57 | #ifndef HAVE_TIMINGSAFE_MEMCMP | ||
58 | int timingsafe_memcmp(const void *b1, const void *b2, size_t len); | ||
59 | #endif | ||
60 | |||
61 | #ifndef HAVE_MEMMEM | ||
62 | void * memmem(const void *big, size_t big_len, const void *little, | ||
63 | size_t little_len); | ||
64 | #endif | ||
65 | |||
66 | #ifdef _WIN32 | ||
67 | #include <errno.h> | ||
68 | |||
69 | static inline char * | ||
70 | posix_strerror(int errnum) | ||
71 | { | ||
72 | if (errnum == ECONNREFUSED) { | ||
73 | return "Connection refused"; | ||
74 | } | ||
75 | return strerror(errnum); | ||
76 | } | ||
77 | |||
78 | #define strerror(errnum) posix_strerror(errnum) | ||
79 | |||
80 | #endif | ||
81 | |||
82 | #endif | ||
diff --git a/include/compat/sys/cdefs.h b/include/compat/sys/cdefs.h new file mode 100644 index 0000000..21ef031 --- /dev/null +++ b/include/compat/sys/cdefs.h | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/cdefs.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_SYS_CDEFS_H | ||
7 | #define LIBCRYPTOCOMPAT_SYS_CDEFS_H | ||
8 | |||
9 | #ifdef _WIN32 | ||
10 | |||
11 | #define __warn_references(sym,msg) | ||
12 | |||
13 | #else | ||
14 | |||
15 | #include_next <sys/cdefs.h> | ||
16 | |||
17 | #ifndef __warn_references | ||
18 | |||
19 | #if defined(__GNUC__) && defined (HAS_GNU_WARNING_LONG) | ||
20 | #define __warn_references(sym,msg) \ | ||
21 | __asm__(".section .gnu.warning." __STRING(sym) \ | ||
22 | " ; .ascii \"" msg "\" ; .text"); | ||
23 | #else | ||
24 | #define __warn_references(sym,msg) | ||
25 | #endif | ||
26 | |||
27 | #endif /* __warn_references */ | ||
28 | |||
29 | #endif /* _WIN32 */ | ||
30 | |||
31 | #endif /* LIBCRYPTOCOMPAT_SYS_CDEFS_H */ | ||
diff --git a/include/compat/sys/ioctl.h b/include/compat/sys/ioctl.h new file mode 100644 index 0000000..a255506 --- /dev/null +++ b/include/compat/sys/ioctl.h | |||
@@ -0,0 +1,11 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/ioctl.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <sys/ioctl.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | #define ioctl(fd, type, arg) ioctlsocket(fd, type, arg) | ||
11 | #endif | ||
diff --git a/include/compat/sys/mman.h b/include/compat/sys/mman.h new file mode 100644 index 0000000..d9eb6a9 --- /dev/null +++ b/include/compat/sys/mman.h | |||
@@ -0,0 +1,19 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/mman.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #include_next <sys/mman.h> | ||
7 | |||
8 | #ifndef LIBCRYPTOCOMPAT_MMAN_H | ||
9 | #define LIBCRYPTOCOMPAT_MMAN_H | ||
10 | |||
11 | #ifndef MAP_ANON | ||
12 | #ifdef MAP_ANONYMOUS | ||
13 | #define MAP_ANON MAP_ANONYMOUS | ||
14 | #else | ||
15 | #error "System does not support mapping anonymous pages?" | ||
16 | #endif | ||
17 | #endif | ||
18 | |||
19 | #endif | ||
diff --git a/include/compat/sys/param.h b/include/compat/sys/param.h new file mode 100644 index 0000000..70488f8 --- /dev/null +++ b/include/compat/sys/param.h | |||
@@ -0,0 +1,15 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/param.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_SYS_PARAM_H | ||
7 | #define LIBCRYPTOCOMPAT_SYS_PARAM_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <winsock2.h> | ||
11 | #else | ||
12 | #include_next <sys/param.h> | ||
13 | #endif | ||
14 | |||
15 | #endif | ||
diff --git a/include/compat/sys/select.h b/include/compat/sys/select.h new file mode 100644 index 0000000..5ca0ea1 --- /dev/null +++ b/include/compat/sys/select.h | |||
@@ -0,0 +1,10 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/select.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <sys/select.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | #endif | ||
diff --git a/include/compat/sys/socket.h b/include/compat/sys/socket.h new file mode 100644 index 0000000..17e84f1 --- /dev/null +++ b/include/compat/sys/socket.h | |||
@@ -0,0 +1,10 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/socket.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <sys/socket.h> | ||
8 | #else | ||
9 | #include <win32netcompat.h> | ||
10 | #endif | ||
diff --git a/include/compat/sys/stat.h b/include/compat/sys/stat.h new file mode 100644 index 0000000..55135d8 --- /dev/null +++ b/include/compat/sys/stat.h | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/stat.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_SYS_STAT_H | ||
7 | #define LIBCRYPTOCOMPAT_SYS_STAT_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <windows.h> | ||
11 | #include <../include/sys/stat.h> | ||
12 | |||
13 | /* File type and permission flags for stat() */ | ||
14 | #if !defined(S_IFMT) | ||
15 | # define S_IFMT _S_IFMT /* File type mask */ | ||
16 | #endif | ||
17 | #if !defined(S_IFDIR) | ||
18 | # define S_IFDIR _S_IFDIR /* Directory */ | ||
19 | #endif | ||
20 | #if !defined(S_IFCHR) | ||
21 | # define S_IFCHR _S_IFCHR /* Character device */ | ||
22 | #endif | ||
23 | #if !defined(S_IFFIFO) | ||
24 | # define S_IFFIFO _S_IFFIFO /* Pipe */ | ||
25 | #endif | ||
26 | #if !defined(S_IFREG) | ||
27 | # define S_IFREG _S_IFREG /* Regular file */ | ||
28 | #endif | ||
29 | #if !defined(S_IREAD) | ||
30 | # define S_IREAD _S_IREAD /* Read permission */ | ||
31 | #endif | ||
32 | #if !defined(S_IWRITE) | ||
33 | # define S_IWRITE _S_IWRITE /* Write permission */ | ||
34 | #endif | ||
35 | #if !defined(S_IEXEC) | ||
36 | # define S_IEXEC _S_IEXEC /* Execute permission */ | ||
37 | #endif | ||
38 | #if !defined(S_IFIFO) | ||
39 | # define S_IFIFO _S_IFIFO /* Pipe */ | ||
40 | #endif | ||
41 | #if !defined(S_IFBLK) | ||
42 | # define S_IFBLK 0 /* Block device */ | ||
43 | #endif | ||
44 | #if !defined(S_IFLNK) | ||
45 | # define S_IFLNK 0 /* Link */ | ||
46 | #endif | ||
47 | #if !defined(S_IFSOCK) | ||
48 | # define S_IFSOCK 0 /* Socket */ | ||
49 | #endif | ||
50 | |||
51 | #if defined(_MSC_VER) | ||
52 | # define S_IRUSR S_IREAD /* Read user */ | ||
53 | # define S_IWUSR S_IWRITE /* Write user */ | ||
54 | # define S_IXUSR 0 /* Execute user */ | ||
55 | # define S_IRGRP 0 /* Read group */ | ||
56 | # define S_IWGRP 0 /* Write group */ | ||
57 | # define S_IXGRP 0 /* Execute group */ | ||
58 | # define S_IROTH 0 /* Read others */ | ||
59 | # define S_IWOTH 0 /* Write others */ | ||
60 | # define S_IXOTH 0 /* Execute others */ | ||
61 | #endif | ||
62 | |||
63 | /* File type flags for d_type */ | ||
64 | #define DT_UNKNOWN 0 | ||
65 | #define DT_REG S_IFREG | ||
66 | #define DT_DIR S_IFDIR | ||
67 | #define DT_FIFO S_IFIFO | ||
68 | #define DT_SOCK S_IFSOCK | ||
69 | #define DT_CHR S_IFCHR | ||
70 | #define DT_BLK S_IFBLK | ||
71 | #define DT_LNK S_IFLNK | ||
72 | |||
73 | /* Macros for converting between st_mode and d_type */ | ||
74 | #define IFTODT(mode) ((mode) & S_IFMT) | ||
75 | #define DTTOIF(type) (type) | ||
76 | |||
77 | /* | ||
78 | * File type macros. Note that block devices, sockets and links cannot be | ||
79 | * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are | ||
80 | * only defined for compatibility. These macros should always return false | ||
81 | * on Windows. | ||
82 | */ | ||
83 | #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) | ||
84 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) | ||
85 | #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) | ||
86 | #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) | ||
87 | #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) | ||
88 | #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) | ||
89 | #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) | ||
90 | |||
91 | #else | ||
92 | #include_next <sys/stat.h> | ||
93 | #endif | ||
94 | |||
95 | #endif | ||
diff --git a/include/compat/sys/time.h b/include/compat/sys/time.h new file mode 100644 index 0000000..235bc6e --- /dev/null +++ b/include/compat/sys/time.h | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/time.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_SYS_TIME_H | ||
7 | #define LIBCRYPTOCOMPAT_SYS_TIME_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <winsock2.h> | ||
11 | int gettimeofday(struct timeval *tp, void *tzp); | ||
12 | #else | ||
13 | #include_next <sys/time.h> | ||
14 | #endif | ||
15 | |||
16 | #endif | ||
diff --git a/include/compat/sys/types.h b/include/compat/sys/types.h new file mode 100644 index 0000000..9929dd5 --- /dev/null +++ b/include/compat/sys/types.h | |||
@@ -0,0 +1,43 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/types.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_SYS_TYPES_H | ||
7 | #define LIBCRYPTOCOMPAT_SYS_TYPES_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <../include/sys/types.h> | ||
11 | #else | ||
12 | #include_next <sys/types.h> | ||
13 | #endif | ||
14 | |||
15 | #include <stdint.h> | ||
16 | |||
17 | #ifdef __MINGW32__ | ||
18 | #include <_bsd_types.h> | ||
19 | #endif | ||
20 | |||
21 | #ifdef _MSC_VER | ||
22 | typedef unsigned char u_char; | ||
23 | typedef unsigned short u_short; | ||
24 | typedef unsigned int u_int; | ||
25 | |||
26 | #include <basetsd.h> | ||
27 | typedef SSIZE_T ssize_t; | ||
28 | |||
29 | #ifndef SSIZE_MAX | ||
30 | #ifdef _WIN64 | ||
31 | #define SSIZE_MAX _I64_MAX | ||
32 | #else | ||
33 | #define SSIZE_MAX INT_MAX | ||
34 | #endif | ||
35 | #endif | ||
36 | |||
37 | #endif | ||
38 | |||
39 | #if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__bounded__) | ||
40 | # define __bounded__(x, y, z) | ||
41 | #endif | ||
42 | |||
43 | #endif | ||
diff --git a/include/compat/sys/uio.h b/include/compat/sys/uio.h new file mode 100644 index 0000000..b4aee9e --- /dev/null +++ b/include/compat/sys/uio.h | |||
@@ -0,0 +1,17 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/select.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include_next <sys/uio.h> | ||
8 | #else | ||
9 | |||
10 | #include <sys/types.h> | ||
11 | |||
12 | struct iovec { | ||
13 | void *iov_base; | ||
14 | size_t iov_len; | ||
15 | }; | ||
16 | |||
17 | #endif | ||
diff --git a/include/compat/time.h b/include/compat/time.h new file mode 100644 index 0000000..d363d42 --- /dev/null +++ b/include/compat/time.h | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * sys/time.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_TIME_H | ||
7 | #define LIBCRYPTOCOMPAT_TIME_H | ||
8 | |||
9 | #ifdef _MSC_VER | ||
10 | #include <../include/time.h> | ||
11 | #define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL) | ||
12 | #else | ||
13 | #include_next <time.h> | ||
14 | #endif | ||
15 | |||
16 | #endif | ||
diff --git a/include/compat/unistd.h b/include/compat/unistd.h new file mode 100644 index 0000000..4676dc8 --- /dev/null +++ b/include/compat/unistd.h | |||
@@ -0,0 +1,36 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * unistd.h compatibility shim | ||
4 | */ | ||
5 | |||
6 | #ifndef LIBCRYPTOCOMPAT_UNISTD_H | ||
7 | #define LIBCRYPTOCOMPAT_UNISTD_H | ||
8 | |||
9 | #ifndef _MSC_VER | ||
10 | #include_next <unistd.h> | ||
11 | #else | ||
12 | |||
13 | #include <stdlib.h> | ||
14 | #include <io.h> | ||
15 | #include <process.h> | ||
16 | |||
17 | #define R_OK 4 | ||
18 | #define W_OK 2 | ||
19 | #define X_OK 0 | ||
20 | #define F_OK 0 | ||
21 | |||
22 | #define access _access | ||
23 | |||
24 | static inline unsigned int sleep(unsigned int seconds) | ||
25 | { | ||
26 | Sleep(seconds * 1000); | ||
27 | return seconds; | ||
28 | } | ||
29 | |||
30 | #endif | ||
31 | |||
32 | #ifndef HAVE_GETENTROPY | ||
33 | int getentropy(void *buf, size_t buflen); | ||
34 | #endif | ||
35 | |||
36 | #endif | ||
diff --git a/include/compat/win32netcompat.h b/include/compat/win32netcompat.h new file mode 100644 index 0000000..452cfba --- /dev/null +++ b/include/compat/win32netcompat.h | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | * | ||
4 | * BSD socket emulation code for Winsock2 | ||
5 | * Brent Cook <bcook@openbsd.org> | ||
6 | */ | ||
7 | |||
8 | #ifndef LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H | ||
9 | #define LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H | ||
10 | |||
11 | #ifdef _WIN32 | ||
12 | |||
13 | #include <ws2tcpip.h> | ||
14 | |||
15 | #define SHUT_RDWR SD_BOTH | ||
16 | #define SHUT_RD SD_RECEIVE | ||
17 | #define SHUT_WR SD_SEND | ||
18 | |||
19 | #include <errno.h> | ||
20 | #include <unistd.h> | ||
21 | |||
22 | int posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); | ||
23 | |||
24 | int posix_close(int fd); | ||
25 | ssize_t posix_read(int fd, void *buf, size_t count); | ||
26 | |||
27 | ssize_t posix_write(int fd, const void *buf, size_t count); | ||
28 | |||
29 | int posix_getsockopt(int sockfd, int level, int optname, | ||
30 | void *optval, socklen_t *optlen); | ||
31 | |||
32 | int posix_setsockopt(int sockfd, int level, int optname, | ||
33 | const void *optval, socklen_t optlen); | ||
34 | |||
35 | #ifndef NO_REDEF_POSIX_FUNCTIONS | ||
36 | #define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen) | ||
37 | #define close(fd) posix_close(fd) | ||
38 | #define read(fd, buf, count) posix_read(fd, buf, count) | ||
39 | #define write(fd, buf, count) posix_write(fd, buf, count) | ||
40 | #define getsockopt(sockfd, level, optname, optval, optlen) \ | ||
41 | posix_getsockopt(sockfd, level, optname, optval, optlen) | ||
42 | #define setsockopt(sockfd, level, optname, optval, optlen) \ | ||
43 | posix_setsockopt(sockfd, level, optname, optval, optlen) | ||
44 | #endif | ||
45 | |||
46 | #endif | ||
47 | |||
48 | #endif | ||