Add compat bits for libtls on Windows

This commit is contained in:
kinichiro 2018-02-25 01:59:39 +09:00
parent 47781e69e2
commit 3681d02253
8 changed files with 110 additions and 0 deletions

View File

@ -21,6 +21,7 @@
#ifdef __MINGW32__ #ifdef __MINGW32__
#include <_bsd_types.h> #include <_bsd_types.h>
typedef uint32_t in_addr_t; typedef uint32_t in_addr_t;
typedef uint32_t uid_t;
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
@ -29,6 +30,7 @@ typedef unsigned short u_short;
typedef unsigned int u_int; typedef unsigned int u_int;
typedef uint32_t in_addr_t; typedef uint32_t in_addr_t;
typedef uint32_t mode_t; typedef uint32_t mode_t;
typedef uint32_t uid_t;
#include <basetsd.h> #include <basetsd.h>
typedef SSIZE_T ssize_t; typedef SSIZE_T ssize_t;

View File

@ -7,7 +7,16 @@
#define LIBCRYPTOCOMPAT_UNISTD_H #define LIBCRYPTOCOMPAT_UNISTD_H
#ifndef _MSC_VER #ifndef _MSC_VER
#include_next <unistd.h> #include_next <unistd.h>
#ifdef __MINGW32__
int ftruncate(int fd, off_t length);
uid_t getuid(void);
ssize_t pread(int d, void *buf, size_t nbytes, off_t offset);
ssize_t pwrite(int d, const void *buf, size_t nbytes, off_t offset);
#endif
#else #else
#include <stdlib.h> #include <stdlib.h>
@ -22,10 +31,19 @@
#define X_OK 0 #define X_OK 0
#define F_OK 0 #define F_OK 0
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define access _access #define access _access
unsigned int sleep(unsigned int seconds); unsigned int sleep(unsigned int seconds);
int ftruncate(int fd, off_t length);
uid_t getuid(void);
ssize_t pread(int d, void *buf, size_t nbytes, off_t offset);
ssize_t pwrite(int d, const void *buf, size_t nbytes, off_t offset);
#endif #endif
#ifndef HAVE_GETENTROPY #ifndef HAVE_GETENTROPY

View File

@ -19,6 +19,16 @@ set(
tls_verify.c tls_verify.c
) )
if(CMAKE_HOST_WIN32)
set(
TLS_SRC
${TLS_SRC}
compat/ftruncate.c
compat/getuid.c
compat/pread.c
compat/pwrite.c
)
endif()
if(NOT "${OPENSSLDIR}" STREQUAL "") if(NOT "${OPENSSLDIR}" STREQUAL "")
add_definitions(-D_PATH_SSL_CA_FILE=\"${OPENSSLDIR}/cert.pem\") add_definitions(-D_PATH_SSL_CA_FILE=\"${OPENSSLDIR}/cert.pem\")

View File

@ -30,3 +30,10 @@ libtls_la_SOURCES += tls_peer.c
libtls_la_SOURCES += tls_util.c libtls_la_SOURCES += tls_util.c
libtls_la_SOURCES += tls_verify.c libtls_la_SOURCES += tls_verify.c
noinst_HEADERS = tls_internal.h noinst_HEADERS = tls_internal.h
if HOST_WIN
libtls_la_SOURCES += compat/ftruncate.c
libtls_la_SOURCES += compat/getuid.c
libtls_la_SOURCES += compat/pread.c
libtls_la_SOURCES += compat/pwrite.c
endif

13
tls/compat/ftruncate.c Normal file
View File

@ -0,0 +1,13 @@
/*
* Public domain
*
* Kinichiro Inoguchi <inoguchi@openbsd.org>
*/
#include <unistd.h>
int
ftruncate(int fd, off_t length)
{
return _chsize(fd, length);
}

14
tls/compat/getuid.c Normal file
View File

@ -0,0 +1,14 @@
/*
* Public domain
*
* Kinichiro Inoguchi <inoguchi@openbsd.org>
*/
#include <unistd.h>
uid_t
getuid(void)
{
/* Windows fstat sets 0 as st_uid */
return 0;
}

23
tls/compat/pread.c Normal file
View File

@ -0,0 +1,23 @@
/*
* Public domain
*
* Kinichiro Inoguchi <inoguchi@openbsd.org>
*/
#include <unistd.h>
ssize_t
pread(int d, void *buf, size_t nbytes, off_t offset)
{
off_t cpos, opos, rpos;
ssize_t bytes;
if((cpos = lseek(d, 0, SEEK_CUR)) == -1)
return -1;
if((opos = lseek(d, offset, SEEK_SET)) == -1)
return -1;
if((bytes = read(d, buf, nbytes)) == -1)
return -1;
if((rpos = lseek(d, cpos, SEEK_SET)) == -1)
return -1;
return bytes;
}

23
tls/compat/pwrite.c Normal file
View File

@ -0,0 +1,23 @@
/*
* Public domain
*
* Kinichiro Inoguchi <inoguchi@openbsd.org>
*/
#include <unistd.h>
ssize_t
pwrite(int d, const void *buf, size_t nbytes, off_t offset)
{
off_t cpos, opos, rpos;
ssize_t bytes;
if((cpos = lseek(d, 0, SEEK_CUR)) == -1)
return -1;
if((opos = lseek(d, offset, SEEK_SET)) == -1)
return -1;
if((bytes = write(d, buf, nbytes)) == -1)
return -1;
if((rpos = lseek(d, cpos, SEEK_SET)) == -1)
return -1;
return bytes;
}