From 5d8a1cf7155130bd8101090d7e1d0c2f90d9b123 Mon Sep 17 00:00:00 2001
From: Brent Cook <busterb@gmail.com>
Date: Thu, 10 Jul 2014 22:06:10 -0500
Subject: 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.
---
 .gitignore                         |   1 +
 CMakeLists.txt                     | 130 +++++++
 Makefile.am                        |   1 +
 Makefile.am.common                 |   2 +-
 apps/CMakeLists.txt                |  84 +++++
 apps/Makefile.am                   |   1 +
 crypto/CMakeLists.txt              | 624 +++++++++++++++++++++++++++++++
 crypto/Makefile.am                 |   3 +
 crypto/compat/posix_win.c          |  24 ++
 include/Makefile.am                |  50 +--
 include/arpa/inet.h                |  19 -
 include/arpa/nameser.h             |  23 --
 include/compat/arpa/inet.h         |  19 +
 include/compat/arpa/nameser.h      |  23 ++
 include/compat/dirent.h            |  17 +
 include/compat/dirent_msvc.h       | 748 +++++++++++++++++++++++++++++++++++++
 include/compat/err.h               |  33 ++
 include/compat/machine/endian.h    |  40 ++
 include/compat/netdb.h             |  10 +
 include/compat/netinet/in.h        |  10 +
 include/compat/netinet/tcp.h       |  10 +
 include/compat/poll.h              |  63 ++++
 include/compat/stdio.h             |  39 ++
 include/compat/stdlib.h            |  34 ++
 include/compat/string.h            |  82 ++++
 include/compat/sys/cdefs.h         |  31 ++
 include/compat/sys/ioctl.h         |  11 +
 include/compat/sys/mman.h          |  19 +
 include/compat/sys/param.h         |  15 +
 include/compat/sys/select.h        |  10 +
 include/compat/sys/socket.h        |  10 +
 include/compat/sys/stat.h          |  95 +++++
 include/compat/sys/time.h          |  16 +
 include/compat/sys/types.h         |  43 +++
 include/compat/sys/uio.h           |  17 +
 include/compat/time.h              |  16 +
 include/compat/unistd.h            |  36 ++
 include/compat/win32netcompat.h    |  48 +++
 include/err.h                      |  33 --
 include/machine/endian.h           |  40 --
 include/netdb.h                    |  10 -
 include/netinet/in.h               |  10 -
 include/netinet/tcp.h              |  10 -
 include/poll.h                     |  63 ----
 include/stdio.h                    |  31 --
 include/stdlib.h                   |  29 --
 include/string.h                   |  73 ----
 include/sys/cdefs.h                |  31 --
 include/sys/ioctl.h                |  11 -
 include/sys/mman.h                 |  19 -
 include/sys/select.h               |  10 -
 include/sys/socket.h               |  10 -
 include/sys/times.h                |  10 -
 include/sys/types.h                |  21 --
 include/sys/uio.h                  |  17 -
 include/syslog.h                   |  38 --
 include/unistd.h                   |  15 -
 include/win32netcompat.h           |  48 ---
 libtls-standalone/include/string.h |  13 +-
 m4/check-os-options.m4             |   2 +-
 patches/arc4random.c.patch         |  15 +
 patches/openssl.c.patch            |  27 +-
 patches/opensslconf.h.patch        |  13 +
 patches/ossl_typ.h.patch           |   4 +-
 patches/pkcs7.h.patch              |   4 +-
 patches/x509.h.patch               |   6 +-
 ssl/CMakeLists.txt                 |  53 +++
 ssl/Makefile.am                    |   1 +
 tls/CMakeLists.txt                 |  22 ++
 tls/Makefile.am                    |   1 +
 update.sh                          |  16 +-
 71 files changed, 2546 insertions(+), 617 deletions(-)
 create mode 100644 CMakeLists.txt
 create mode 100644 apps/CMakeLists.txt
 create mode 100644 crypto/CMakeLists.txt
 delete mode 100644 include/arpa/inet.h
 delete mode 100644 include/arpa/nameser.h
 create mode 100644 include/compat/arpa/inet.h
 create mode 100644 include/compat/arpa/nameser.h
 create mode 100644 include/compat/dirent.h
 create mode 100644 include/compat/dirent_msvc.h
 create mode 100644 include/compat/err.h
 create mode 100644 include/compat/machine/endian.h
 create mode 100644 include/compat/netdb.h
 create mode 100644 include/compat/netinet/in.h
 create mode 100644 include/compat/netinet/tcp.h
 create mode 100644 include/compat/poll.h
 create mode 100644 include/compat/stdio.h
 create mode 100644 include/compat/stdlib.h
 create mode 100644 include/compat/string.h
 create mode 100644 include/compat/sys/cdefs.h
 create mode 100644 include/compat/sys/ioctl.h
 create mode 100644 include/compat/sys/mman.h
 create mode 100644 include/compat/sys/param.h
 create mode 100644 include/compat/sys/select.h
 create mode 100644 include/compat/sys/socket.h
 create mode 100644 include/compat/sys/stat.h
 create mode 100644 include/compat/sys/time.h
 create mode 100644 include/compat/sys/types.h
 create mode 100644 include/compat/sys/uio.h
 create mode 100644 include/compat/time.h
 create mode 100644 include/compat/unistd.h
 create mode 100644 include/compat/win32netcompat.h
 delete mode 100644 include/err.h
 delete mode 100644 include/machine/endian.h
 delete mode 100644 include/netdb.h
 delete mode 100644 include/netinet/in.h
 delete mode 100644 include/netinet/tcp.h
 delete mode 100644 include/poll.h
 delete mode 100644 include/stdio.h
 delete mode 100644 include/stdlib.h
 delete mode 100644 include/string.h
 delete mode 100644 include/sys/cdefs.h
 delete mode 100644 include/sys/ioctl.h
 delete mode 100644 include/sys/mman.h
 delete mode 100644 include/sys/select.h
 delete mode 100644 include/sys/socket.h
 delete mode 100644 include/sys/times.h
 delete mode 100644 include/sys/types.h
 delete mode 100644 include/sys/uio.h
 delete mode 100644 include/syslog.h
 delete mode 100644 include/unistd.h
 delete mode 100644 include/win32netcompat.h
 create mode 100644 patches/arc4random.c.patch
 create mode 100644 patches/opensslconf.h.patch
 create mode 100644 ssl/CMakeLists.txt
 create mode 100644 tls/CMakeLists.txt

diff --git a/.gitignore b/.gitignore
index 9c2d7da..7efedef 100644
--- a/.gitignore
+++ b/.gitignore
@@ -124,6 +124,7 @@ include/openssl/*.he
 !/crypto/compat/bsd_asprintf.c
 !/crypto/compat/inet_pton.c
 !/crypto/compat/ui_openssl_win.c
+!/crypto/CMakeLists.txt
 
 /libtls-standalone/include/*.h
 /libtls-standalone/src/*.c
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..e642bcc
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,130 @@
+cmake_minimum_required (VERSION 3.2)
+include(CheckFunctionExists)
+
+project (LibreSSL)
+
+if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
+	add_definitions(-DHAVE_ATTRIBUTE__BOUNDED__)
+endif()
+
+add_definitions(-DLIBRESSL_INTERNAL)
+add_definitions(-DOPENSSL_NO_HW_PADLOCK)
+add_definitions(-DOPENSSL_NO_ASM)
+
+if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
+	add_definitions(-Wno-pointer-sign)
+endif()
+
+if(MSVC)
+	add_definitions(-Dinline=__inline)
+	add_definitions(-Drestrict)
+	add_definitions(-D_CRT_SECURE_NO_WARNINGS)
+	add_definitions(-D_CRT_DEPRECATED_NO_WARNINGS)
+	add_definitions(-D_REENTRANT -D_POSIX_THREAD_SAFE_FUNCTIONS)
+	add_definitions(-DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0501)
+	add_definitions(-DCPPFLAGS -DOPENSSL_NO_SPEED -DNO_SYSLOG -DNO_CRYPT)
+
+	set(MSVC_DISABLED_WARNINGS_LIST
+		"C4057" # C4057: 'initializing' : 'unsigned char *' differs in
+		        # indirection to slightly different base types from 'char [2]'
+		"C4100" # 'exarg' : unreferenced formal parameter
+		"C4127" # conditional expression is constant
+		"C4242" # 'function' : conversion from 'int' to 'uint8_t',
+		        # possible loss of data
+		"C4244" # 'function' : conversion from 'int' to 'uint8_t',
+		        # possible loss of data
+		"C4706" # assignment within conditional expression
+		"C4820" # 'bytes' bytes padding added after construct 'member_name'
+		"C4996" # 'read': The POSIX name for this item is deprecated. Instead,
+		        # use the ISO C++ conformant name: _read.
+	)
+	string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
+		${MSVC_DISABLED_WARNINGS_LIST})
+	set(CMAKE_C_FLAGS  "-MP -W4 ${MSVC_DISABLED_WARNINGS_STR}")
+endif()
+
+check_function_exists(asprintf HAVE_ASPRINTF)
+if(HAVE_ASPRINTF)
+	add_definitions(-DHAVE_ASPRINTF)
+endif()
+
+check_function_exists(inet_pton HAVE_INET_PTON)
+if(HAVE_INET_PTON)
+	add_definitions(-DHAVE_INET_PTON)
+endif()
+
+check_function_exists(reallocarray HAVE_REALLOCARRAY)
+if(HAVE_REALLOCARRAY)
+	add_definitions(-DHAVE_REALLOCARRAY)
+endif()
+
+check_function_exists(strcasecmp HAVE_STRCASECMP)
+if(HAVE_STRCASECMP)
+	add_definitions(-DHAVE_STRCASECMP)
+endif()
+
+check_function_exists(strlcat HAVE_STRLCAT)
+if(HAVE_STRLCAT)
+	add_definitions(-DHAVE_STRLCAT)
+endif()
+
+check_function_exists(strlcat HAVE_STRLCPY)
+if(HAVE_STRLCPY)
+	add_definitions(-DHAVE_STRLCPY)
+endif()
+
+check_function_exists(strndup HAVE_STRNDUP)
+if(HAVE_STRNDUP)
+	add_definitions(-DHAVE_STRNDUP)
+endif()
+
+if(MSVC)
+	set(HAVE_STRNLEN)
+	add_definitions(-DHAVE_STRNLEN)
+else()
+	check_function_exists(strnlen HAVE_STRNLEN)
+	if(HAVE_STRNLEN)
+		add_definitions(-DHAVE_STRNLEN)
+	endif()
+endif()
+
+check_function_exists(strsep HAVE_STRSEP)
+if(HAVE_STRSEP)
+	add_definitions(-DHAVE_STRSEP)
+endif()
+
+check_function_exists(arc4random_buf HAVE_ARC4RANDOM_BUF)
+if(HAVE_ARC4RANDOM_BUF)
+	add_definitions(-DHAVE_ARC4RANDOM_BUF)
+endif()
+
+check_function_exists(explicit_bzero HAVE_EXPLICIT_BZERO)
+if(HAVE_EXPLICIT_BZERO)
+	add_definitions(-DHAVE_EXPLICIT_BZERO)
+endif()
+
+check_function_exists(getauxval HAVE_GETAUXVAL)
+if(HAVE_GETAUXVAL)
+	add_definitions(-DHAVE_GETAUXVAL)
+endif()
+
+check_function_exists(getentropy HAVE_GETENTROPY)
+if(HAVE_GETENTROPY)
+	add_definitions(-DHAVE_GETENTROPY)
+endif()
+
+check_function_exists(timingsafe_bcmp HAVE_TIMINGSAFE_BCMP)
+if(HAVE_TIMINGSAFE_BCMP)
+	add_definitions(-DHAVE_TIMINGSAFE_BCMP)
+endif()
+
+check_function_exists(timingsafe_memcmp HAVE_TIMINGSAFE_MEMCMP)
+if(HAVE_MEMCMP)
+	add_definitions(-DHAVE_MEMCMP)
+endif()
+
+add_subdirectory(crypto)
+add_subdirectory(ssl)
+add_subdirectory(apps)
+add_subdirectory(tls)
+add_subdirectory(tests)
diff --git a/Makefile.am b/Makefile.am
index 46b7644..0804984 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,3 +5,4 @@ pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libcrypto.pc libssl.pc libtls.pc openssl.pc
 
 EXTRA_DIST = README.md README.windows VERSION config scripts
+EXTRA_DIST += CMakeLists.txt
diff --git a/Makefile.am.common b/Makefile.am.common
index 7a25d09..b00473d 100644
--- a/Makefile.am.common
+++ b/Makefile.am.common
@@ -1,2 +1,2 @@
-AM_CFLAGS = -I$(top_srcdir)/include
+AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/include/compat
 AM_CPPFLAGS = -DLIBRESSL_INTERNAL
diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt
new file mode 100644
index 0000000..13d6e9c
--- /dev/null
+++ b/apps/CMakeLists.txt
@@ -0,0 +1,84 @@
+include_directories(
+	.
+	../include
+	../include/compat
+)
+
+set(
+	OPENSSL_SRC
+	apps.c
+	asn1pars.c
+	ca.c
+	ciphers.c
+	cms.c
+	crl.c
+	crl2p7.c
+	dgst.c
+	dh.c
+	dhparam.c
+	dsa.c
+	dsaparam.c
+	ec.c
+	ecparam.c
+	enc.c
+	engine.c
+	errstr.c
+	gendh.c
+	gendsa.c
+	genpkey.c
+	genrsa.c
+	nseq.c
+	ocsp.c
+	openssl.c
+	passwd.c
+	pkcs12.c
+	pkcs7.c
+	pkcs8.c
+	pkey.c
+	pkeyparam.c
+	pkeyutl.c
+	prime.c
+	rand.c
+	req.c
+	rsa.c
+	rsautl.c
+	s_cb.c
+	s_client.c
+	s_server.c
+	s_socket.c
+	s_time.c
+	sess_id.c
+	smime.c
+	speed.c
+	spkac.c
+	ts.c
+	verify.c
+	version.c
+	x509.c
+)
+
+if(CMAKE_HOST_UNIX)
+	set(OPENSSL_SRC ${OPENSSL_SRC} apps_posix.c)
+	set(OPENSSL_SRC ${OPENSSL_SRC} certhash.c)
+endif()
+
+if(CMAKE_HOST_WIN32)
+	set(OPENSSL_SRC ${OPENSSL_SRC} apps_win.c)
+	set(OPENSSL_SRC ${OPENSSL_SRC} certhash_disabled.c)
+	set(OPENSSL_SRC ${OPENSSL_SRC} poll_win.c)
+endif()
+
+check_function_exists(strtonum HAVE_STRTONUM)
+if(HAVE_STRTONUM)
+	add_definitions(-DHAVE_STRTONUM)
+else()
+	set(OPENSSL_SRC ${OPENSSL_SRC} strtonum.c)
+endif()
+
+set(OPENSSL_LIBS ssl crypto)
+if(CMAKE_HOST_WIN32)
+	set(OPENSSL_LIBS ${OPENSSL_LIBS} ws2_32)
+endif()
+
+add_executable(openssl ${OPENSSL_SRC})
+target_link_libraries(openssl ${OPENSSL_LIBS})
diff --git a/apps/Makefile.am b/apps/Makefile.am
index 0547876..ce78b33 100644
--- a/apps/Makefile.am
+++ b/apps/Makefile.am
@@ -88,6 +88,7 @@ noinst_HEADERS += timeouts.h
 EXTRA_DIST = cert.pem
 EXTRA_DIST += openssl.cnf
 EXTRA_DIST += x509v3.cnf
+EXTRA_DIST += CMakeLists.txt
 
 install-exec-hook:
 	@if [ "@OPENSSLDIR@x" != "x" ]; then \
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
new file mode 100644
index 0000000..1a058c4
--- /dev/null
+++ b/crypto/CMakeLists.txt
@@ -0,0 +1,624 @@
+include_directories(
+	.
+	../include
+	../include/compat
+	asn1
+	dsa
+	evp
+	modes
+)
+
+set(
+	CRYPTO_SRC
+
+	aes/aes_cbc.c
+	aes/aes_core.c
+	camellia/camellia.c
+	camellia/cmll_cbc.c
+	rc4/rc4_enc.c
+	rc4/rc4_skey.c
+	whrlpool/wp_block.c
+	cpt_err.c
+	cryptlib.c
+	cversion.c
+	ex_data.c
+	malloc-wrapper.c
+	mem_clr.c
+	mem_dbg.c
+	o_init.c
+	o_str.c
+	o_time.c
+	aes/aes_cfb.c
+	aes/aes_ctr.c
+	aes/aes_ecb.c
+	aes/aes_ige.c
+	aes/aes_misc.c
+	aes/aes_ofb.c
+	aes/aes_wrap.c
+	asn1/a_bitstr.c
+	asn1/a_bool.c
+	asn1/a_bytes.c
+	asn1/a_d2i_fp.c
+	asn1/a_digest.c
+	asn1/a_dup.c
+	asn1/a_enum.c
+	asn1/a_gentm.c
+	asn1/a_i2d_fp.c
+	asn1/a_int.c
+	asn1/a_mbstr.c
+	asn1/a_object.c
+	asn1/a_octet.c
+	asn1/a_print.c
+	asn1/a_set.c
+	asn1/a_sign.c
+	asn1/a_strex.c
+	asn1/a_strnid.c
+	asn1/a_time.c
+	asn1/a_type.c
+	asn1/a_utctm.c
+	asn1/a_utf8.c
+	asn1/a_verify.c
+	asn1/ameth_lib.c
+	asn1/asn1_err.c
+	asn1/asn1_gen.c
+	asn1/asn1_lib.c
+	asn1/asn1_par.c
+	asn1/asn_mime.c
+	asn1/asn_moid.c
+	asn1/asn_pack.c
+	asn1/bio_asn1.c
+	asn1/bio_ndef.c
+	asn1/d2i_pr.c
+	asn1/d2i_pu.c
+	asn1/evp_asn1.c
+	asn1/f_enum.c
+	asn1/f_int.c
+	asn1/f_string.c
+	asn1/i2d_pr.c
+	asn1/i2d_pu.c
+	asn1/n_pkey.c
+	asn1/nsseq.c
+	asn1/p5_pbe.c
+	asn1/p5_pbev2.c
+	asn1/p8_pkey.c
+	asn1/t_bitst.c
+	asn1/t_crl.c
+	asn1/t_pkey.c
+	asn1/t_req.c
+	asn1/t_spki.c
+	asn1/t_x509.c
+	asn1/t_x509a.c
+	asn1/tasn_dec.c
+	asn1/tasn_enc.c
+	asn1/tasn_fre.c
+	asn1/tasn_new.c
+	asn1/tasn_prn.c
+	asn1/tasn_typ.c
+	asn1/tasn_utl.c
+	asn1/x_algor.c
+	asn1/x_attrib.c
+	asn1/x_bignum.c
+	asn1/x_crl.c
+	asn1/x_exten.c
+	asn1/x_info.c
+	asn1/x_long.c
+	asn1/x_name.c
+	asn1/x_nx509.c
+	asn1/x_pkey.c
+	asn1/x_pubkey.c
+	asn1/x_req.c
+	asn1/x_sig.c
+	asn1/x_spki.c
+	asn1/x_val.c
+	asn1/x_x509.c
+	asn1/x_x509a.c
+	bf/bf_cfb64.c
+	bf/bf_ecb.c
+	bf/bf_enc.c
+	bf/bf_ofb64.c
+	bf/bf_skey.c
+	bio/b_dump.c
+	bio/b_print.c
+	bio/b_sock.c
+	bio/bf_buff.c
+	bio/bf_nbio.c
+	bio/bf_null.c
+	bio/bio_cb.c
+	bio/bio_err.c
+	bio/bio_lib.c
+	bio/bss_acpt.c
+	bio/bss_bio.c
+	bio/bss_conn.c
+	bio/bss_dgram.c
+	bio/bss_fd.c
+	bio/bss_file.c
+	bio/bss_mem.c
+	bio/bss_null.c
+	bio/bss_sock.c
+	bn/bn_add.c
+	bn/bn_asm.c
+	bn/bn_blind.c
+	bn/bn_const.c
+	bn/bn_ctx.c
+	bn/bn_depr.c
+	bn/bn_div.c
+	bn/bn_err.c
+	bn/bn_exp.c
+	bn/bn_exp2.c
+	bn/bn_gcd.c
+	bn/bn_gf2m.c
+	bn/bn_kron.c
+	bn/bn_lib.c
+	bn/bn_mod.c
+	bn/bn_mont.c
+	bn/bn_mpi.c
+	bn/bn_mul.c
+	bn/bn_nist.c
+	bn/bn_prime.c
+	bn/bn_print.c
+	bn/bn_rand.c
+	bn/bn_recp.c
+	bn/bn_shift.c
+	bn/bn_sqr.c
+	bn/bn_sqrt.c
+	bn/bn_word.c
+	bn/bn_x931p.c
+	buffer/buf_err.c
+	buffer/buf_str.c
+	buffer/buffer.c
+	camellia/cmll_cfb.c
+	camellia/cmll_ctr.c
+	camellia/cmll_ecb.c
+	camellia/cmll_misc.c
+	camellia/cmll_ofb.c
+	cast/c_cfb64.c
+	cast/c_ecb.c
+	cast/c_enc.c
+	cast/c_ofb64.c
+	cast/c_skey.c
+	chacha/chacha.c
+	cmac/cm_ameth.c
+	cmac/cm_pmeth.c
+	cmac/cmac.c
+	comp/c_rle.c
+	comp/c_zlib.c
+	comp/comp_err.c
+	comp/comp_lib.c
+	conf/conf_api.c
+	conf/conf_def.c
+	conf/conf_err.c
+	conf/conf_lib.c
+	conf/conf_mall.c
+	conf/conf_mod.c
+	conf/conf_sap.c
+	des/cbc_cksm.c
+	des/cbc_enc.c
+	des/cfb64ede.c
+	des/cfb64enc.c
+	des/cfb_enc.c
+	des/des_enc.c
+	des/ecb3_enc.c
+	des/ecb_enc.c
+	des/ede_cbcm_enc.c
+	des/enc_read.c
+	des/enc_writ.c
+	des/fcrypt.c
+	des/fcrypt_b.c
+	des/ofb64ede.c
+	des/ofb64enc.c
+	des/ofb_enc.c
+	des/pcbc_enc.c
+	des/qud_cksm.c
+	des/rand_key.c
+	des/set_key.c
+	des/str2key.c
+	des/xcbc_enc.c
+	dh/dh_ameth.c
+	dh/dh_asn1.c
+	dh/dh_check.c
+	dh/dh_depr.c
+	dh/dh_err.c
+	dh/dh_gen.c
+	dh/dh_key.c
+	dh/dh_lib.c
+	dh/dh_pmeth.c
+	dh/dh_prn.c
+	dsa/dsa_ameth.c
+	dsa/dsa_asn1.c
+	dsa/dsa_depr.c
+	dsa/dsa_err.c
+	dsa/dsa_gen.c
+	dsa/dsa_key.c
+	dsa/dsa_lib.c
+	dsa/dsa_ossl.c
+	dsa/dsa_pmeth.c
+	dsa/dsa_prn.c
+	dsa/dsa_sign.c
+	dsa/dsa_vrf.c
+	dso/dso_dlfcn.c
+	dso/dso_err.c
+	dso/dso_lib.c
+	dso/dso_null.c
+	dso/dso_openssl.c
+	ec/ec2_mult.c
+	ec/ec2_oct.c
+	ec/ec2_smpl.c
+	ec/ec_ameth.c
+	ec/ec_asn1.c
+	ec/ec_check.c
+	ec/ec_curve.c
+	ec/ec_cvt.c
+	ec/ec_err.c
+	ec/ec_key.c
+	ec/ec_lib.c
+	ec/ec_mult.c
+	ec/ec_oct.c
+	ec/ec_pmeth.c
+	ec/ec_print.c
+	ec/eck_prn.c
+	ec/ecp_mont.c
+	ec/ecp_nist.c
+	ec/ecp_oct.c
+	ec/ecp_smpl.c
+	ecdh/ech_err.c
+	ecdh/ech_key.c
+	ecdh/ech_lib.c
+	ecdh/ech_ossl.c
+	ecdsa/ecs_asn1.c
+	ecdsa/ecs_err.c
+	ecdsa/ecs_lib.c
+	ecdsa/ecs_ossl.c
+	ecdsa/ecs_sign.c
+	ecdsa/ecs_vrf.c
+	engine/eng_all.c
+	engine/eng_cnf.c
+	engine/eng_ctrl.c
+	engine/eng_dyn.c
+	engine/eng_err.c
+	engine/eng_fat.c
+	engine/eng_init.c
+	engine/eng_lib.c
+	engine/eng_list.c
+	engine/eng_openssl.c
+	engine/eng_pkey.c
+	engine/eng_rsax.c
+	engine/eng_table.c
+	engine/tb_asnmth.c
+	engine/tb_cipher.c
+	engine/tb_dh.c
+	engine/tb_digest.c
+	engine/tb_dsa.c
+	engine/tb_ecdh.c
+	engine/tb_ecdsa.c
+	engine/tb_pkmeth.c
+	engine/tb_rand.c
+	engine/tb_rsa.c
+	engine/tb_store.c
+	err/err.c
+	err/err_all.c
+	err/err_prn.c
+	evp/bio_b64.c
+	evp/bio_enc.c
+	evp/bio_md.c
+	evp/c_all.c
+	evp/digest.c
+	evp/e_aes.c
+	evp/e_aes_cbc_hmac_sha1.c
+	evp/e_bf.c
+	evp/e_camellia.c
+	evp/e_cast.c
+	evp/e_chacha.c
+	evp/e_chacha20poly1305.c
+	evp/e_des.c
+	evp/e_des3.c
+	evp/e_gost2814789.c
+	evp/e_idea.c
+	evp/e_null.c
+	evp/e_old.c
+	evp/e_rc2.c
+	evp/e_rc4.c
+	evp/e_rc4_hmac_md5.c
+	evp/e_xcbc_d.c
+	evp/encode.c
+	evp/evp_aead.c
+	evp/evp_enc.c
+	evp/evp_err.c
+	evp/evp_key.c
+	evp/evp_lib.c
+	evp/evp_pbe.c
+	evp/evp_pkey.c
+	evp/m_dss.c
+	evp/m_dss1.c
+	evp/m_ecdsa.c
+	evp/m_gost2814789.c
+	evp/m_gostr341194.c
+	evp/m_md4.c
+	evp/m_md5.c
+	evp/m_null.c
+	evp/m_ripemd.c
+	evp/m_sha.c
+	evp/m_sha1.c
+	evp/m_sigver.c
+	evp/m_streebog.c
+	evp/m_wp.c
+	evp/names.c
+	evp/p5_crpt.c
+	evp/p5_crpt2.c
+	evp/p_dec.c
+	evp/p_enc.c
+	evp/p_lib.c
+	evp/p_open.c
+	evp/p_seal.c
+	evp/p_sign.c
+	evp/p_verify.c
+	evp/pmeth_fn.c
+	evp/pmeth_gn.c
+	evp/pmeth_lib.c
+	gost/gost2814789.c
+	gost/gost89_keywrap.c
+	gost/gost89_params.c
+	gost/gost89imit_ameth.c
+	gost/gost89imit_pmeth.c
+	gost/gost_asn1.c
+	gost/gost_err.c
+	gost/gostr341001.c
+	gost/gostr341001_ameth.c
+	gost/gostr341001_key.c
+	gost/gostr341001_params.c
+	gost/gostr341001_pmeth.c
+	gost/gostr341194.c
+	gost/streebog.c
+	hmac/hm_ameth.c
+	hmac/hm_pmeth.c
+	hmac/hmac.c
+	idea/i_cbc.c
+	idea/i_cfb64.c
+	idea/i_ecb.c
+	idea/i_ofb64.c
+	idea/i_skey.c
+	krb5/krb5_asn.c
+	lhash/lh_stats.c
+	lhash/lhash.c
+	md4/md4_dgst.c
+	md4/md4_one.c
+	md5/md5_dgst.c
+	md5/md5_one.c
+	modes/cbc128.c
+	modes/ccm128.c
+	modes/cfb128.c
+	modes/ctr128.c
+	modes/cts128.c
+	modes/gcm128.c
+	modes/ofb128.c
+	modes/xts128.c
+	objects/o_names.c
+	objects/obj_dat.c
+	objects/obj_err.c
+	objects/obj_lib.c
+	objects/obj_xref.c
+	ocsp/ocsp_asn.c
+	ocsp/ocsp_cl.c
+	ocsp/ocsp_err.c
+	ocsp/ocsp_ext.c
+	ocsp/ocsp_ht.c
+	ocsp/ocsp_lib.c
+	ocsp/ocsp_prn.c
+	ocsp/ocsp_srv.c
+	ocsp/ocsp_vfy.c
+	pem/pem_all.c
+	pem/pem_err.c
+	pem/pem_info.c
+	pem/pem_lib.c
+	pem/pem_oth.c
+	pem/pem_pk8.c
+	pem/pem_pkey.c
+	pem/pem_seal.c
+	pem/pem_sign.c
+	pem/pem_x509.c
+	pem/pem_xaux.c
+	pem/pvkfmt.c
+	pkcs12/p12_add.c
+	pkcs12/p12_asn.c
+	pkcs12/p12_attr.c
+	pkcs12/p12_crpt.c
+	pkcs12/p12_crt.c
+	pkcs12/p12_decr.c
+	pkcs12/p12_init.c
+	pkcs12/p12_key.c
+	pkcs12/p12_kiss.c
+	pkcs12/p12_mutl.c
+	pkcs12/p12_npas.c
+	pkcs12/p12_p8d.c
+	pkcs12/p12_p8e.c
+	pkcs12/p12_utl.c
+	pkcs12/pk12err.c
+	pkcs7/bio_pk7.c
+	pkcs7/pk7_asn1.c
+	pkcs7/pk7_attr.c
+	pkcs7/pk7_doit.c
+	pkcs7/pk7_lib.c
+	pkcs7/pk7_mime.c
+	pkcs7/pk7_smime.c
+	pkcs7/pkcs7err.c
+	poly1305/poly1305.c
+	rand/rand_err.c
+	rand/rand_lib.c
+	rand/randfile.c
+	rc2/rc2_cbc.c
+	rc2/rc2_ecb.c
+	rc2/rc2_skey.c
+	rc2/rc2cfb64.c
+	rc2/rc2ofb64.c
+	ripemd/rmd_dgst.c
+	ripemd/rmd_one.c
+	rsa/rsa_ameth.c
+	rsa/rsa_asn1.c
+	rsa/rsa_chk.c
+	rsa/rsa_crpt.c
+	rsa/rsa_depr.c
+	rsa/rsa_eay.c
+	rsa/rsa_err.c
+	rsa/rsa_gen.c
+	rsa/rsa_lib.c
+	rsa/rsa_none.c
+	rsa/rsa_oaep.c
+	rsa/rsa_pk1.c
+	rsa/rsa_pmeth.c
+	rsa/rsa_prn.c
+	rsa/rsa_pss.c
+	rsa/rsa_saos.c
+	rsa/rsa_sign.c
+	rsa/rsa_ssl.c
+	rsa/rsa_x931.c
+	sha/sha1_one.c
+	sha/sha1dgst.c
+	sha/sha256.c
+	sha/sha512.c
+	sha/sha_dgst.c
+	sha/sha_one.c
+	stack/stack.c
+	ts/ts_asn1.c
+	ts/ts_conf.c
+	ts/ts_err.c
+	ts/ts_lib.c
+	ts/ts_req_print.c
+	ts/ts_req_utils.c
+	ts/ts_rsp_print.c
+	ts/ts_rsp_sign.c
+	ts/ts_rsp_utils.c
+	ts/ts_rsp_verify.c
+	ts/ts_verify_ctx.c
+	txt_db/txt_db.c
+	ui/ui_err.c
+	ui/ui_lib.c
+	ui/ui_util.c
+	whrlpool/wp_dgst.c
+	x509/by_dir.c
+	x509/by_file.c
+	x509/by_mem.c
+	x509/x509_att.c
+	x509/x509_cmp.c
+	x509/x509_d2.c
+	x509/x509_def.c
+	x509/x509_err.c
+	x509/x509_ext.c
+	x509/x509_lu.c
+	x509/x509_obj.c
+	x509/x509_r2x.c
+	x509/x509_req.c
+	x509/x509_set.c
+	x509/x509_trs.c
+	x509/x509_txt.c
+	x509/x509_v3.c
+	x509/x509_vfy.c
+	x509/x509_vpm.c
+	x509/x509cset.c
+	x509/x509name.c
+	x509/x509rset.c
+	x509/x509spki.c
+	x509/x509type.c
+	x509/x_all.c
+	x509v3/pcy_cache.c
+	x509v3/pcy_data.c
+	x509v3/pcy_lib.c
+	x509v3/pcy_map.c
+	x509v3/pcy_node.c
+	x509v3/pcy_tree.c
+	x509v3/v3_akey.c
+	x509v3/v3_akeya.c
+	x509v3/v3_alt.c
+	x509v3/v3_bcons.c
+	x509v3/v3_bitst.c
+	x509v3/v3_conf.c
+	x509v3/v3_cpols.c
+	x509v3/v3_crld.c
+	x509v3/v3_enum.c
+	x509v3/v3_extku.c
+	x509v3/v3_genn.c
+	x509v3/v3_ia5.c
+	x509v3/v3_info.c
+	x509v3/v3_int.c
+	x509v3/v3_lib.c
+	x509v3/v3_ncons.c
+	x509v3/v3_ocsp.c
+	x509v3/v3_pci.c
+	x509v3/v3_pcia.c
+	x509v3/v3_pcons.c
+	x509v3/v3_pku.c
+	x509v3/v3_pmaps.c
+	x509v3/v3_prn.c
+	x509v3/v3_purp.c
+	x509v3/v3_skey.c
+	x509v3/v3_sxnet.c
+	x509v3/v3_utl.c
+	x509v3/v3err.c
+)
+
+if(CMAKE_HOST_UNIX)
+	set(CRYPTO_SRC ${CRYPTO_SRC} bio/b_posix.c)
+	set(CRYPTO_SRC ${CRYPTO_SRC} bio/bss_log.c)
+	set(CRYPTO_SRC ${CRYPTO_SRC} ui/ui_openssl.c)
+endif()
+
+if(CMAKE_HOST_WIN32)
+	set(CRYPTO_SRC ${CRYPTO_SRC} bio/b_win.c)
+	set(CRYPTO_SRC ${CRYPTO_SRC} ui/ui_openssl_win.c)
+endif()
+
+if(CMAKE_HOST_WIN32)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/posix_win.c)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/arc4random.c)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/getentropy_win.c)
+endif()
+
+if(NOT HAVE_ASPRINTF)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/bsd-asprintf.c)
+endif()
+
+if(NOT HAVE_INET_PTON)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/inet_pton.c)
+endif()
+
+if(NOT HAVE_REALLOCARRAY)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/reallocarray.c)
+endif()
+
+if(NOT HAVE_STRCASECMP)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/strcasecmp.c)
+endif()
+
+if(NOT HAVE_STRLCAT)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/strlcat.c)
+endif()
+
+if(NOT HAVE_STRLCPY)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/strlcpy.c)
+endif()
+
+if(NOT HAVE_STRNDUP)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/strndup.c)
+	if(NOT HAVE_STRNLEN)
+		set(CRYPTO_SRC ${CRYPTO_SRC} compat/strnlen.c)
+	endif()
+endif()
+
+if(NOT HAVE_EXPLICIT_BZERO)
+	if(CMAKE_HOST_WIN32)
+		set(CRYPTO_SRC ${CRYPTO_SRC} compat/explicit_bzero_win.c)
+	else()
+		set(CRYPTO_SRC ${CRYPTO_SRC} compat/explicit_bzero.c)
+	endif()
+endif()
+
+if(NOT HAVE_GETENTROPY)
+endif()
+
+if(NOT HAVE_TIMINGSAFE_BCMP)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/timingsafe_bcmp.c)
+endif()
+
+if(NOT HAVE_TIMINGSAFE_MEMCMP)
+	set(CRYPTO_SRC ${CRYPTO_SRC} compat/timingsafe_memcmp.c)
+endif()
+
+add_library(crypto ${CRYPTO_SRC})
diff --git a/crypto/Makefile.am b/crypto/Makefile.am
index 1799969..b988d77 100644
--- a/crypto/Makefile.am
+++ b/crypto/Makefile.am
@@ -7,6 +7,7 @@ AM_CFLAGS += -I$(top_srcdir)/crypto/modes
 lib_LTLIBRARIES = libcrypto.la
 
 EXTRA_DIST = VERSION
+EXTRA_DIST += CMakeLists.txt
 
 libcrypto_la_LDFLAGS = -version-info @LIBCRYPTO_VERSION@ -no-undefined
 libcrypto_la_LIBADD = libcompat.la libcompatnoopt.la
@@ -236,7 +237,9 @@ libcrypto_la_SOURCES += bio/bss_conn.c
 libcrypto_la_SOURCES += bio/bss_dgram.c
 libcrypto_la_SOURCES += bio/bss_fd.c
 libcrypto_la_SOURCES += bio/bss_file.c
+if !HOST_WIN
 libcrypto_la_SOURCES += bio/bss_log.c
+endif
 libcrypto_la_SOURCES += bio/bss_mem.c
 libcrypto_la_SOURCES += bio/bss_null.c
 libcrypto_la_SOURCES += bio/bss_sock.c
diff --git a/crypto/compat/posix_win.c b/crypto/compat/posix_win.c
index 855406b..f8a46ab 100644
--- a/crypto/compat/posix_win.c
+++ b/crypto/compat/posix_win.c
@@ -166,3 +166,27 @@ posix_setsockopt(int sockfd, int level, int optname,
 	int rc = setsockopt(sockfd, level, optname, (char *)optval, optlen);
 	return rc == 0 ? 0 : wsa_errno(WSAGetLastError());
 }
+
+#ifdef _MSC_VER
+int gettimeofday(struct timeval * tp, struct timezone * tzp)
+{
+	/*
+	 * Note: some broken versions only have 8 trailing zero's, the correct
+	 * epoch has 9 trailing zero's
+	 */
+	static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
+
+	SYSTEMTIME  system_time;
+	FILETIME    file_time;
+	uint64_t    time;
+
+	GetSystemTime(&system_time);
+	SystemTimeToFileTime(&system_time, &file_time);
+	time = ((uint64_t)file_time.dwLowDateTime);
+	time += ((uint64_t)file_time.dwHighDateTime) << 32;
+
+	tp->tv_sec = (long)((time - EPOCH) / 10000000L);
+	tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
+	return 0;
+}
+#endif
diff --git a/include/Makefile.am b/include/Makefile.am
index 92ee9e7..c24d1ae 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -2,32 +2,36 @@ include $(top_srcdir)/Makefile.am.common
 
 SUBDIRS = openssl
 
-noinst_HEADERS = err.h
-noinst_HEADERS += netdb.h
-noinst_HEADERS += poll.h
-noinst_HEADERS += pqueue.h
-noinst_HEADERS += stdio.h
-noinst_HEADERS += stdlib.h
-noinst_HEADERS += string.h
-noinst_HEADERS += syslog.h
-noinst_HEADERS += unistd.h
-noinst_HEADERS += win32netcompat.h
+noinst_HEADERS = pqueue.h
+noinst_HEADERS += compat/dirent.h
+noinst_HEADERS += compat/dirent_msvc.h
+noinst_HEADERS += compat/err.h
+noinst_HEADERS += compat/netdb.h
+noinst_HEADERS += compat/poll.h
+noinst_HEADERS += compat/stdio.h
+noinst_HEADERS += compat/stdlib.h
+noinst_HEADERS += compat/string.h
+noinst_HEADERS += compat/time.h
+noinst_HEADERS += compat/unistd.h
+noinst_HEADERS += compat/win32netcompat.h
 
-noinst_HEADERS += arpa/inet.h
-noinst_HEADERS += arpa/nameser.h
+noinst_HEADERS += compat/arpa/inet.h
+noinst_HEADERS += compat/arpa/nameser.h
 
-noinst_HEADERS += machine/endian.h
+noinst_HEADERS += compat/machine/endian.h
 
-noinst_HEADERS += netinet/in.h
-noinst_HEADERS += netinet/tcp.h
+noinst_HEADERS += compat/netinet/in.h
+noinst_HEADERS += compat/netinet/tcp.h
 
-noinst_HEADERS += sys/cdefs.h
-noinst_HEADERS += sys/ioctl.h
-noinst_HEADERS += sys/mman.h
-noinst_HEADERS += sys/select.h
-noinst_HEADERS += sys/socket.h
-noinst_HEADERS += sys/times.h
-noinst_HEADERS += sys/types.h
-noinst_HEADERS += sys/uio.h
+noinst_HEADERS += compat/sys/cdefs.h
+noinst_HEADERS += compat/sys/ioctl.h
+noinst_HEADERS += compat/sys/mman.h
+noinst_HEADERS += compat/sys/param.h
+noinst_HEADERS += compat/sys/select.h
+noinst_HEADERS += compat/sys/stat.h
+noinst_HEADERS += compat/sys/socket.h
+noinst_HEADERS += compat/sys/time.h
+noinst_HEADERS += compat/sys/types.h
+noinst_HEADERS += compat/sys/uio.h
 
 include_HEADERS = tls.h
diff --git a/include/arpa/inet.h b/include/arpa/inet.h
deleted file mode 100644
index 67740c6..0000000
--- a/include/arpa/inet.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Public domain
- * arpa/inet.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <arpa/inet.h>
-#else
-#include <win32netcompat.h>
-
-#ifndef AI_ADDRCONFIG
-#define AI_ADDRCONFIG               0x00000400
-#endif
-
-#endif
-
-#ifndef HAVE_INET_PTON
-int inet_pton(int af, const char * restrict src, void * restrict dst);
-#endif
diff --git a/include/arpa/nameser.h b/include/arpa/nameser.h
deleted file mode 100644
index 0126a60..0000000
--- a/include/arpa/nameser.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Public domain
- * arpa/inet.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <arpa/nameser.h>
-#else
-#include <win32netcompat.h>
-
-#ifndef INADDRSZ
-#define INADDRSZ 4
-#endif
-
-#ifndef IN6ADDRSZ
-#define IN6ADDRSZ 16
-#endif
-
-#ifndef INT16SZ
-#define INT16SZ	2
-#endif
-
-#endif
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 @@
+/*
+ * Public domain
+ * arpa/inet.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <arpa/inet.h>
+#else
+#include <win32netcompat.h>
+
+#ifndef AI_ADDRCONFIG
+#define AI_ADDRCONFIG               0x00000400
+#endif
+
+#endif
+
+#ifndef HAVE_INET_PTON
+int inet_pton(int af, const char * src, void * dst);
+#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 @@
+/*
+ * Public domain
+ * arpa/inet.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <arpa/nameser.h>
+#else
+#include <win32netcompat.h>
+
+#ifndef INADDRSZ
+#define INADDRSZ 4
+#endif
+
+#ifndef IN6ADDRSZ
+#define IN6ADDRSZ 16
+#endif
+
+#ifndef INT16SZ
+#define INT16SZ	2
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * dirent.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_DIRENT_H
+#define LIBCRYPTOCOMPAT_DIRENT_H
+
+#ifdef _MSC_VER
+#include <windows.h>
+#include <dirent_msvc.h>
+#else
+#include_next <dirent.h>
+#endif
+
+#endif
+
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 @@
+/*
+ * dirent.h - dirent API for Microsoft Visual Studio
+ *
+ * Copyright (C) 2006-2012 Toni Ronkko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * ``Software''), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * $Id: dirent.h,v 1.20 2014/03/19 17:52:23 tronkko Exp $
+ */
+#ifndef DIRENT_MSVC_H
+#define DIRENT_MSVC_H
+
+#include <windows.h>
+
+#include <../include/stdio.h>
+#include <../include/stdarg.h>
+#include <../include/wchar.h>
+#include <../include/string.h>
+#include <../include/stdlib.h>
+#include <../include/malloc.h>
+#include <../include/sys/types.h>
+#include <sys/stat.h>
+#include <../include/errno.h>
+
+/* Indicates that d_type field is available in dirent structure */
+#define _DIRENT_HAVE_D_TYPE
+
+/* Indicates that d_namlen field is available in dirent structure */
+#define _DIRENT_HAVE_D_NAMLEN
+
+/* Entries missing from MSVC 6.0 */
+#if !defined(FILE_ATTRIBUTE_DEVICE)
+#   define FILE_ATTRIBUTE_DEVICE 0x40
+#endif
+
+/* Maximum length of file name */
+#if !defined(PATH_MAX)
+#   define PATH_MAX MAX_PATH
+#endif
+#if !defined(FILENAME_MAX)
+#   define FILENAME_MAX MAX_PATH
+#endif
+#if !defined(NAME_MAX)
+#   define NAME_MAX FILENAME_MAX
+#endif
+
+/* Return the exact length of d_namlen without zero terminator */
+#define _D_EXACT_NAMLEN(p) ((p)->d_namlen)
+
+/* Return number of bytes needed to store d_namlen */
+#define _D_ALLOC_NAMLEN(p) (PATH_MAX)
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Wide-character version */
+struct _wdirent {
+    long d_ino;                                 /* Always zero */
+    unsigned short d_reclen;                    /* Structure size */
+    size_t d_namlen;                            /* Length of name without \0 */
+    int d_type;                                 /* File type */
+    wchar_t d_name[PATH_MAX];                   /* File name */
+};
+typedef struct _wdirent _wdirent;
+
+struct _WDIR {
+    struct _wdirent ent;                        /* Current directory entry */
+    WIN32_FIND_DATAW data;                      /* Private file data */
+    int cached;                                 /* True if data is valid */
+    HANDLE handle;                              /* Win32 search handle */
+    wchar_t *patt;                              /* Initial directory name */
+};
+typedef struct _WDIR _WDIR;
+
+static _WDIR *_wopendir (const wchar_t *dirname);
+static struct _wdirent *_wreaddir (_WDIR *dirp);
+static int _wclosedir (_WDIR *dirp);
+static void _wrewinddir (_WDIR* dirp);
+
+
+/* For compatibility with Symbian */
+#define wdirent _wdirent
+#define WDIR _WDIR
+#define wopendir _wopendir
+#define wreaddir _wreaddir
+#define wclosedir _wclosedir
+#define wrewinddir _wrewinddir
+
+
+/* Multi-byte character versions */
+struct dirent {
+    long d_ino;                                 /* Always zero */
+    unsigned short d_reclen;                    /* Structure size */
+    size_t d_namlen;                            /* Length of name without \0 */
+    int d_type;                                 /* File type */
+    char d_name[PATH_MAX];                      /* File name */
+};
+typedef struct dirent dirent;
+
+struct DIR {
+    struct dirent ent;
+    struct _WDIR *wdirp;
+};
+typedef struct DIR DIR;
+
+static DIR *opendir (const char *dirname);
+static struct dirent *readdir (DIR *dirp);
+static int closedir (DIR *dirp);
+static void rewinddir (DIR* dirp);
+
+
+/* Internal utility functions */
+static WIN32_FIND_DATAW *dirent_first (_WDIR *dirp);
+static WIN32_FIND_DATAW *dirent_next (_WDIR *dirp);
+
+static int dirent_mbstowcs_s(
+    size_t *pReturnValue,
+    wchar_t *wcstr,
+    size_t sizeInWords,
+    const char *mbstr,
+    size_t count);
+
+static int dirent_wcstombs_s(
+    size_t *pReturnValue,
+    char *mbstr,
+    size_t sizeInBytes,
+    const wchar_t *wcstr,
+    size_t count);
+
+static void dirent_set_errno (int error);
+
+/*
+ * Open directory stream DIRNAME for read and return a pointer to the
+ * internal working area that is used to retrieve individual directory
+ * entries.
+ */
+static _WDIR*
+_wopendir(
+    const wchar_t *dirname)
+{
+    _WDIR *dirp = NULL;
+    int error;
+
+    /* Must have directory name */
+    if (dirname == NULL  ||  dirname[0] == '\0') {
+        dirent_set_errno (ENOENT);
+        return NULL;
+    }
+
+    /* Allocate new _WDIR structure */
+    dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
+    if (dirp != NULL) {
+        DWORD n;
+
+        /* Reset _WDIR structure */
+        dirp->handle = INVALID_HANDLE_VALUE;
+        dirp->patt = NULL;
+        dirp->cached = 0;
+
+        /* Compute the length of full path plus zero terminator */
+        n = GetFullPathNameW (dirname, 0, NULL, NULL);
+
+        /* Allocate room for absolute directory name and search pattern */
+        dirp->patt = (wchar_t*) malloc (sizeof (wchar_t) * n + 16);
+        if (dirp->patt) {
+
+            /*
+             * Convert relative directory name to an absolute one.  This
+             * allows rewinddir() to function correctly even when current
+             * working directory is changed between opendir() and rewinddir().
+             */
+            n = GetFullPathNameW (dirname, n, dirp->patt, NULL);
+            if (n > 0) {
+                wchar_t *p;
+
+                /* Append search pattern \* to the directory name */
+                p = dirp->patt + n;
+                if (dirp->patt < p) {
+                    switch (p[-1]) {
+                    case '\\':
+                    case '/':
+                    case ':':
+                        /* Directory ends in path separator, e.g. c:\temp\ */
+                        /*NOP*/;
+                        break;
+
+                    default:
+                        /* Directory name doesn't end in path separator */
+                        *p++ = '\\';
+                    }
+                }
+                *p++ = '*';
+                *p = '\0';
+
+                /* Open directory stream and retrieve the first entry */
+                if (dirent_first (dirp)) {
+                    /* Directory stream opened successfully */
+                    error = 0;
+                } else {
+                    /* Cannot retrieve first entry */
+                    error = 1;
+                    dirent_set_errno (ENOENT);
+                }
+
+            } else {
+                /* Cannot retrieve full path name */
+                dirent_set_errno (ENOENT);
+                error = 1;
+            }
+
+        } else {
+            /* Cannot allocate memory for search pattern */
+            error = 1;
+        }
+
+    } else {
+        /* Cannot allocate _WDIR structure */
+        error = 1;
+    }
+
+    /* Clean up in case of error */
+    if (error  &&  dirp) {
+        _wclosedir (dirp);
+        dirp = NULL;
+    }
+
+    return dirp;
+}
+
+/*
+ * Read next directory entry.  The directory entry is returned in dirent
+ * structure in the d_name field.  Individual directory entries returned by
+ * this function include regular files, sub-directories, pseudo-directories
+ * "." and ".." as well as volume labels, hidden files and system files.
+ */
+static struct _wdirent*
+_wreaddir(
+    _WDIR *dirp)
+{
+    WIN32_FIND_DATAW *datap;
+    struct _wdirent *entp;
+
+    /* Read next directory entry */
+    datap = dirent_next (dirp);
+    if (datap) {
+        size_t n;
+        DWORD attr;
+
+        /* Pointer to directory entry to return */
+        entp = &dirp->ent;
+
+        /*
+         * Copy file name as wide-character string.  If the file name is too
+         * long to fit in to the destination buffer, then truncate file name
+         * to PATH_MAX characters and zero-terminate the buffer.
+         */
+        n = 0;
+        while (n + 1 < PATH_MAX  &&  datap->cFileName[n] != 0) {
+            entp->d_name[n] = datap->cFileName[n];
+            n++;
+        }
+        dirp->ent.d_name[n] = 0;
+
+        /* Length of file name excluding zero terminator */
+        entp->d_namlen = n;
+
+        /* File type */
+        attr = datap->dwFileAttributes;
+        if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
+            entp->d_type = DT_CHR;
+        } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
+            entp->d_type = DT_DIR;
+        } else {
+            entp->d_type = DT_REG;
+        }
+
+        /* Reset dummy fields */
+        entp->d_ino = 0;
+        entp->d_reclen = sizeof (struct _wdirent);
+
+    } else {
+
+        /* Last directory entry read */
+        entp = NULL;
+
+    }
+
+    return entp;
+}
+
+/*
+ * Close directory stream opened by opendir() function.  This invalidates the
+ * DIR structure as well as any directory entry read previously by
+ * _wreaddir().
+ */
+static int
+_wclosedir(
+    _WDIR *dirp)
+{
+    int ok;
+    if (dirp) {
+
+        /* Release search handle */
+        if (dirp->handle != INVALID_HANDLE_VALUE) {
+            FindClose (dirp->handle);
+            dirp->handle = INVALID_HANDLE_VALUE;
+        }
+
+        /* Release search pattern */
+        if (dirp->patt) {
+            free (dirp->patt);
+            dirp->patt = NULL;
+        }
+
+        /* Release directory structure */
+        free (dirp);
+        ok = /*success*/0;
+
+    } else {
+        /* Invalid directory stream */
+        dirent_set_errno (EBADF);
+        ok = /*failure*/-1;
+    }
+    return ok;
+}
+
+/*
+ * Rewind directory stream such that _wreaddir() returns the very first
+ * file name again.
+ */
+static void
+_wrewinddir(
+    _WDIR* dirp)
+{
+    if (dirp) {
+        /* Release existing search handle */
+        if (dirp->handle != INVALID_HANDLE_VALUE) {
+            FindClose (dirp->handle);
+        }
+
+        /* Open new search handle */
+        dirent_first (dirp);
+    }
+}
+
+/* Get first directory entry (internal) */
+static WIN32_FIND_DATAW*
+dirent_first(
+    _WDIR *dirp)
+{
+    WIN32_FIND_DATAW *datap;
+
+    /* Open directory and retrieve the first entry */
+    dirp->handle = FindFirstFileW (dirp->patt, &dirp->data);
+    if (dirp->handle != INVALID_HANDLE_VALUE) {
+
+        /* a directory entry is now waiting in memory */
+        datap = &dirp->data;
+        dirp->cached = 1;
+
+    } else {
+
+        /* Failed to re-open directory: no directory entry in memory */
+        dirp->cached = 0;
+        datap = NULL;
+
+    }
+    return datap;
+}
+
+/* Get next directory entry (internal) */
+static WIN32_FIND_DATAW*
+dirent_next(
+    _WDIR *dirp)
+{
+    WIN32_FIND_DATAW *p;
+
+    /* Get next directory entry */
+    if (dirp->cached != 0) {
+
+        /* A valid directory entry already in memory */
+        p = &dirp->data;
+        dirp->cached = 0;
+
+    } else if (dirp->handle != INVALID_HANDLE_VALUE) {
+
+        /* Get the next directory entry from stream */
+        if (FindNextFileW (dirp->handle, &dirp->data) != FALSE) {
+            /* Got a file */
+            p = &dirp->data;
+        } else {
+            /* The very last entry has been processed or an error occured */
+            FindClose (dirp->handle);
+            dirp->handle = INVALID_HANDLE_VALUE;
+            p = NULL;
+        }
+
+    } else {
+
+        /* End of directory stream reached */
+        p = NULL;
+
+    }
+
+    return p;
+}
+
+/*
+ * Open directory stream using plain old C-string.
+ */
+static DIR*
+opendir(
+    const char *dirname)
+{
+    struct DIR *dirp;
+    int error;
+
+    /* Must have directory name */
+    if (dirname == NULL  ||  dirname[0] == '\0') {
+        dirent_set_errno (ENOENT);
+        return NULL;
+    }
+
+    /* Allocate memory for DIR structure */
+    dirp = (DIR*) malloc (sizeof (struct DIR));
+    if (dirp) {
+        wchar_t wname[PATH_MAX];
+        size_t n;
+
+        /* Convert directory name to wide-character string */
+        error = dirent_mbstowcs_s (&n, wname, PATH_MAX, dirname, PATH_MAX);
+        if (!error) {
+
+            /* Open directory stream using wide-character name */
+            dirp->wdirp = _wopendir (wname);
+            if (dirp->wdirp) {
+                /* Directory stream opened */
+                error = 0;
+            } else {
+                /* Failed to open directory stream */
+                error = 1;
+            }
+
+        } else {
+            /*
+             * Cannot convert file name to wide-character string.  This
+             * occurs if the string contains invalid multi-byte sequences or
+             * the output buffer is too small to contain the resulting
+             * string.
+             */
+            error = 1;
+        }
+
+    } else {
+        /* Cannot allocate DIR structure */
+        error = 1;
+    }
+
+    /* Clean up in case of error */
+    if (error  &&  dirp) {
+        free (dirp);
+        dirp = NULL;
+    }
+
+    return dirp;
+}
+
+/*
+ * Read next directory entry.
+ *
+ * When working with text consoles, please note that file names returned by
+ * readdir() are represented in the default ANSI code page while any output to
+ * console is typically formatted on another code page.  Thus, non-ASCII
+ * characters in file names will not usually display correctly on console.  The
+ * problem can be fixed in two ways: (1) change the character set of console
+ * to 1252 using chcp utility and use Lucida Console font, or (2) use
+ * _cprintf function when writing to console.  The _cprinf() will re-encode
+ * ANSI strings to the console code page so many non-ASCII characters will
+ * display correcly.
+ */
+static struct dirent*
+readdir(
+    DIR *dirp)
+{
+    WIN32_FIND_DATAW *datap;
+    struct dirent *entp;
+
+    /* Read next directory entry */
+    datap = dirent_next (dirp->wdirp);
+    if (datap) {
+        size_t n;
+        int error;
+
+        /* Attempt to convert file name to multi-byte string */
+        error = dirent_wcstombs_s(
+            &n, dirp->ent.d_name, PATH_MAX, datap->cFileName, PATH_MAX);
+
+        /*
+         * If the file name cannot be represented by a multi-byte string,
+         * then attempt to use old 8+3 file name.  This allows traditional
+         * Unix-code to access some file names despite of unicode
+         * characters, although file names may seem unfamiliar to the user.
+         *
+         * Be ware that the code below cannot come up with a short file
+         * name unless the file system provides one.  At least
+         * VirtualBox shared folders fail to do this.
+         */
+        if (error  &&  datap->cAlternateFileName[0] != '\0') {
+            error = dirent_wcstombs_s(
+                &n, dirp->ent.d_name, PATH_MAX,
+                datap->cAlternateFileName, PATH_MAX);
+        }
+
+        if (!error) {
+            DWORD attr;
+
+            /* Initialize directory entry for return */
+            entp = &dirp->ent;
+
+            /* Length of file name excluding zero terminator */
+            entp->d_namlen = n - 1;
+
+            /* File attributes */
+            attr = datap->dwFileAttributes;
+            if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
+                entp->d_type = DT_CHR;
+            } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
+                entp->d_type = DT_DIR;
+            } else {
+                entp->d_type = DT_REG;
+            }
+
+            /* Reset dummy fields */
+            entp->d_ino = 0;
+            entp->d_reclen = sizeof (struct dirent);
+
+        } else {
+            /*
+             * Cannot convert file name to multi-byte string so construct
+             * an errornous directory entry and return that.  Note that
+             * we cannot return NULL as that would stop the processing
+             * of directory entries completely.
+             */
+            entp = &dirp->ent;
+            entp->d_name[0] = '?';
+            entp->d_name[1] = '\0';
+            entp->d_namlen = 1;
+            entp->d_type = DT_UNKNOWN;
+            entp->d_ino = 0;
+            entp->d_reclen = 0;
+        }
+
+    } else {
+        /* No more directory entries */
+        entp = NULL;
+    }
+
+    return entp;
+}
+
+/*
+ * Close directory stream.
+ */
+static int
+closedir(
+    DIR *dirp)
+{
+    int ok;
+    if (dirp) {
+
+        /* Close wide-character directory stream */
+        ok = _wclosedir (dirp->wdirp);
+        dirp->wdirp = NULL;
+
+        /* Release multi-byte character version */
+        free (dirp);
+
+    } else {
+
+        /* Invalid directory stream */
+        dirent_set_errno (EBADF);
+        ok = /*failure*/-1;
+
+    }
+    return ok;
+}
+
+/*
+ * Rewind directory stream to beginning.
+ */
+static void
+rewinddir(
+    DIR* dirp)
+{
+    /* Rewind wide-character string directory stream */
+    _wrewinddir (dirp->wdirp);
+}
+
+/* Convert multi-byte string to wide character string */
+static int
+dirent_mbstowcs_s(
+    size_t *pReturnValue,
+    wchar_t *wcstr,
+    size_t sizeInWords,
+    const char *mbstr,
+    size_t count)
+{
+    int error;
+
+#if defined(_MSC_VER)  &&  _MSC_VER >= 1400
+
+    /* Microsoft Visual Studio 2005 or later */
+    error = mbstowcs_s (pReturnValue, wcstr, sizeInWords, mbstr, count);
+
+#else
+
+    /* Older Visual Studio or non-Microsoft compiler */
+    size_t n;
+
+    /* Convert to wide-character string (or count characters) */
+    n = mbstowcs (wcstr, mbstr, sizeInWords);
+    if (!wcstr  ||  n < count) {
+
+        /* Zero-terminate output buffer */
+        if (wcstr  &&  sizeInWords) {
+            if (n >= sizeInWords) {
+                n = sizeInWords - 1;
+            }
+            wcstr[n] = 0;
+        }
+
+        /* Length of resuting multi-byte string WITH zero terminator */
+        if (pReturnValue) {
+            *pReturnValue = n + 1;
+        }
+
+        /* Success */
+        error = 0;
+
+    } else {
+
+        /* Could not convert string */
+        error = 1;
+
+    }
+
+#endif
+
+    return error;
+}
+
+/* Convert wide-character string to multi-byte string */
+static int
+dirent_wcstombs_s(
+    size_t *pReturnValue,
+    char *mbstr,
+    size_t sizeInBytes, /* max size of mbstr */
+    const wchar_t *wcstr,
+    size_t count)
+{
+    int error;
+
+#if defined(_MSC_VER)  &&  _MSC_VER >= 1400
+
+    /* Microsoft Visual Studio 2005 or later */
+    error = wcstombs_s (pReturnValue, mbstr, sizeInBytes, wcstr, count);
+
+#else
+
+    /* Older Visual Studio or non-Microsoft compiler */
+    size_t n;
+
+    /* Convert to multi-byte string (or count the number of bytes needed) */
+    n = wcstombs (mbstr, wcstr, sizeInBytes);
+    if (!mbstr  ||  n < count) {
+
+        /* Zero-terminate output buffer */
+        if (mbstr  &&  sizeInBytes) {
+            if (n >= sizeInBytes) {
+                n = sizeInBytes - 1;
+            }
+            mbstr[n] = '\0';
+        }
+
+        /* Lenght of resulting multi-bytes string WITH zero-terminator */
+        if (pReturnValue) {
+            *pReturnValue = n + 1;
+        }
+
+        /* Success */
+        error = 0;
+
+    } else {
+
+        /* Cannot convert string */
+        error = 1;
+
+    }
+
+#endif
+
+    return error;
+}
+
+/* Set errno variable */
+static void
+dirent_set_errno(
+    int error)
+{
+#if defined(_MSC_VER)  &&  _MSC_VER >= 1400
+
+    /* Microsoft Visual Studio 2005 and later */
+    _set_errno (error);
+
+#else
+
+    /* Non-Microsoft compiler or older Microsoft compiler */
+    errno = error;
+
+#endif
+}
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif /*DIRENT_H*/
+
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 @@
+/*
+ * Public domain
+ * err.h compatibility shim
+ */
+
+#ifdef HAVE_ERR_H
+
+#include_next <err.h>
+
+#else
+
+#ifndef LIBCRYPTOCOMPAT_ERR_H
+#define LIBCRYPTOCOMPAT_ERR_H
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#define err(exitcode, format, ...) \
+  errx(exitcode, format ": %s", __VA_ARGS__, strerror(errno))
+
+#define errx(exitcode, format, ...) \
+  do { warnx(format, __VA_ARGS__); exit(exitcode); } while (0)
+
+#define warn(format, ...) \
+  warnx(format ": %s", __VA_ARGS__, strerror(errno))
+
+#define warnx(format, ...) \
+  fprintf(stderr, format "\n", __VA_ARGS__)
+
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * machine/endian.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_BYTE_ORDER_H_
+#define LIBCRYPTOCOMPAT_BYTE_ORDER_H_
+
+#if defined(_WIN32)
+
+#define LITTLE_ENDIAN  1234
+#define BIG_ENDIAN 4321
+#define PDP_ENDIAN	3412
+
+/*
+ * Use GCC and Visual Studio compiler defines to determine endian.
+ */
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define BYTE_ORDER LITTLE_ENDIAN
+#else
+#define BYTE_ORDER BIG_ENDIAN
+#endif
+
+#elif defined(__linux__)
+#include <endian.h>
+
+#elif defined(__sun) || defined(_AIX) || defined(__hpux)
+#include <sys/types.h>
+#include <arpa/nameser_compat.h>
+
+#elif defined(__sgi)
+#include <standards.h>
+#include <sys/endian.h>
+
+#else
+#include_next <machine/endian.h>
+
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * netdb.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <netdb.h>
+#else
+#include <win32netcompat.h>
+#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 @@
+/*
+ * Public domain
+ * netinet/in.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <netinet/in.h>
+#else
+#include <win32netcompat.h>
+#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 @@
+/*
+ * Public domain
+ * netinet/tcp.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <netinet/tcp.h>
+#else
+#include <win32netcompat.h>
+#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 @@
+/*
+ * Public domain
+ *
+ * poll(2) emulation for Windows
+ *
+ * This emulates just-enough poll functionality on Windows to work in the
+ * context of the openssl(1) program. This is not a replacement for
+ * POSIX.1-2001 poll(2).
+ *
+ * Dongsheng Song <dongsheng.song@gmail.com>
+ * Brent Cook <bcook@openbsd.org>
+ */
+
+#ifndef LIBCRYPTOCOMPAT_POLL_H
+#define LIBCRYPTOCOMPAT_POLL_H
+
+#ifndef _WIN32
+#include_next <poll.h>
+#else
+
+#include <winsock2.h>
+
+/* Type used for the number of file descriptors. */
+typedef unsigned long int nfds_t;
+
+#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600)
+/* Data structure describing a polling request. */
+struct pollfd {
+	int fd; /* file descriptor */
+	short events; /* requested events */
+	short revents; /* returned events */
+};
+
+/* Event types that can be polled */
+#define POLLIN 0x001 /* There is data to read. */
+#define POLLPRI 0x002 /* There is urgent data to read. */
+#define POLLOUT 0x004 /* Writing now will not block. */
+
+# define POLLRDNORM 0x040 /* Normal data may be read. */
+# define POLLRDBAND 0x080 /* Priority data may be read. */
+# define POLLWRNORM 0x100 /* Writing now will not block. */
+# define POLLWRBAND 0x200 /* Priority data may be written. */
+
+/* Event types always implicitly polled. */
+#define POLLERR 0x008 /* Error condition. */
+#define POLLHUP 0x010 /* Hung up. */
+#define POLLNVAL 0x020 /* Invalid polling request. */
+
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int poll(struct pollfd *pfds, nfds_t nfds, int timeout);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAVE_POLL */
+
+#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 @@
+/*
+ * Public domain
+ * stdio.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_STDIO_H
+#define LIBCRYPTOCOMPAT_STDIO_H
+
+#ifdef _MSC_VER
+#include <../include/stdio.h>
+#else
+#include_next <stdio.h>
+#endif
+
+#ifndef HAVE_ASPRINTF
+#include <stdarg.h>
+int vasprintf(char **str, const char *fmt, va_list ap);
+int asprintf(char **str, const char *fmt, ...);
+#endif
+
+#ifdef _WIN32
+
+void posix_perror(const char *s);
+FILE * posix_fopen(const char *path, const char *mode);
+int posix_rename(const char *oldpath, const char *newpath);
+
+#ifndef NO_REDEF_POSIX_FUNCTIONS
+#define perror(errnum) posix_perror(errnum)
+#define fopen(path, mode) posix_fopen(path, mode)
+#define rename(oldpath, newpath) posix_rename(oldpath, newpath)
+#endif
+
+#ifdef _MSC_VER
+#define snprintf _snprintf
+#endif
+
+#endif
+
+#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 @@
+/*
+ * stdlib.h compatibility shim
+ * Public domain
+ */
+
+#ifndef LIBCRYPTOCOMPAT_STDLIB_H
+#define LIBCRYPTOCOMPAT_STDLIB_H
+
+#ifdef _MSC_VER
+#include <../include/stdlib.h>
+#else
+#include_next <stdlib.h>
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+//#include <sys/time.h>
+#include <stdint.h>
+
+#ifndef HAVE_ARC4RANDOM_BUF
+uint32_t arc4random(void);
+void arc4random_buf(void *_buf, size_t n);
+#endif
+
+#ifndef HAVE_REALLOCARRAY
+void *reallocarray(void *, size_t, size_t);
+#endif
+
+#ifndef HAVE_STRTONUM
+long long strtonum(const char *nptr, long long minval,
+		long long maxval, const char **errstr);
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * string.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_STRING_H
+#define LIBCRYPTOCOMPAT_STRING_H
+
+#ifdef _MSC_VER
+#include <../include/string.h>
+#else
+#include_next <string.h>
+#endif
+
+#include <sys/types.h>
+
+#if defined(__sun) || defined(__hpux)
+/* Some functions historically defined in string.h were placed in strings.h by
+ * SUS. Use the same hack as OS X and FreeBSD use to work around on Solaris and HPUX.
+ */
+#include <strings.h>
+#endif
+
+#ifndef HAVE_STRCASECMP
+int strcasecmp(const char *s1, const char *s2);
+int strncasecmp(const char *s1, const char *s2, size_t len);
+#endif
+
+#ifndef HAVE_STRLCPY
+size_t strlcpy(char *dst, const char *src, size_t siz);
+#endif
+
+#ifndef HAVE_STRLCAT
+size_t strlcat(char *dst, const char *src, size_t siz);
+#endif
+
+#ifndef HAVE_STRNDUP
+char * strndup(const char *str, size_t maxlen);
+/* the only user of strnlen is strndup, so only build it if needed */
+#ifndef HAVE_STRNLEN
+size_t strnlen(const char *str, size_t maxlen);
+#endif
+#endif
+
+#ifndef HAVE_STRSEP
+char *strsep(char **stringp, const char *delim);
+#endif
+
+#ifndef HAVE_EXPLICIT_BZERO
+void explicit_bzero(void *, size_t);
+#endif
+
+#ifndef HAVE_TIMINGSAFE_BCMP
+int timingsafe_bcmp(const void *b1, const void *b2, size_t n);
+#endif
+
+#ifndef HAVE_TIMINGSAFE_MEMCMP
+int timingsafe_memcmp(const void *b1, const void *b2, size_t len);
+#endif
+
+#ifndef HAVE_MEMMEM
+void * memmem(const void *big, size_t big_len, const void *little,
+	size_t little_len);
+#endif
+
+#ifdef _WIN32
+#include <errno.h>
+
+static inline char  *
+posix_strerror(int errnum)
+{
+	if (errnum == ECONNREFUSED) {
+		return "Connection refused";
+	}
+	return strerror(errnum);
+}
+
+#define strerror(errnum) posix_strerror(errnum)
+
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * sys/cdefs.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_SYS_CDEFS_H
+#define LIBCRYPTOCOMPAT_SYS_CDEFS_H
+
+#ifdef _WIN32
+
+#define __warn_references(sym,msg)
+
+#else
+
+#include_next <sys/cdefs.h>
+
+#ifndef __warn_references
+
+#if defined(__GNUC__)  && defined (HAS_GNU_WARNING_LONG)
+#define __warn_references(sym,msg)          \
+  __asm__(".section .gnu.warning." __STRING(sym)  \
+         " ; .ascii \"" msg "\" ; .text");
+#else
+#define __warn_references(sym,msg)
+#endif
+
+#endif /* __warn_references */
+
+#endif /* _WIN32 */
+
+#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 @@
+/*
+ * Public domain
+ * sys/ioctl.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <sys/ioctl.h>
+#else
+#include <win32netcompat.h>
+#define ioctl(fd, type, arg) ioctlsocket(fd, type, arg)
+#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 @@
+/*
+ * Public domain
+ * sys/mman.h compatibility shim
+ */
+
+#include_next <sys/mman.h>
+
+#ifndef LIBCRYPTOCOMPAT_MMAN_H
+#define LIBCRYPTOCOMPAT_MMAN_H
+
+#ifndef MAP_ANON
+#ifdef MAP_ANONYMOUS
+#define MAP_ANON MAP_ANONYMOUS
+#else
+#error "System does not support mapping anonymous pages?"
+#endif
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * sys/param.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_SYS_PARAM_H
+#define LIBCRYPTOCOMPAT_SYS_PARAM_H
+
+#ifdef _MSC_VER
+#include <winsock2.h>
+#else
+#include_next <sys/param.h>
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * sys/select.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <sys/select.h>
+#else
+#include <win32netcompat.h>
+#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 @@
+/*
+ * Public domain
+ * sys/socket.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <sys/socket.h>
+#else
+#include <win32netcompat.h>
+#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 @@
+/*
+ * Public domain
+ * sys/stat.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_SYS_STAT_H
+#define LIBCRYPTOCOMPAT_SYS_STAT_H
+
+#ifdef _MSC_VER
+#include <windows.h>
+#include <../include/sys/stat.h>
+
+/* File type and permission flags for stat() */
+#if !defined(S_IFMT)
+#   define S_IFMT   _S_IFMT                     /* File type mask */
+#endif
+#if !defined(S_IFDIR)
+#   define S_IFDIR  _S_IFDIR                    /* Directory */
+#endif
+#if !defined(S_IFCHR)
+#   define S_IFCHR  _S_IFCHR                    /* Character device */
+#endif
+#if !defined(S_IFFIFO)
+#   define S_IFFIFO _S_IFFIFO                   /* Pipe */
+#endif
+#if !defined(S_IFREG)
+#   define S_IFREG  _S_IFREG                    /* Regular file */
+#endif
+#if !defined(S_IREAD)
+#   define S_IREAD  _S_IREAD                    /* Read permission */
+#endif
+#if !defined(S_IWRITE)
+#   define S_IWRITE _S_IWRITE                   /* Write permission */
+#endif
+#if !defined(S_IEXEC)
+#   define S_IEXEC  _S_IEXEC                    /* Execute permission */
+#endif
+#if !defined(S_IFIFO)
+#   define S_IFIFO _S_IFIFO                     /* Pipe */
+#endif
+#if !defined(S_IFBLK)
+#   define S_IFBLK   0                          /* Block device */
+#endif
+#if !defined(S_IFLNK)
+#   define S_IFLNK   0                          /* Link */
+#endif
+#if !defined(S_IFSOCK)
+#   define S_IFSOCK  0                          /* Socket */
+#endif
+
+#if defined(_MSC_VER)
+#   define S_IRUSR  S_IREAD                     /* Read user */
+#   define S_IWUSR  S_IWRITE                    /* Write user */
+#   define S_IXUSR  0                           /* Execute user */
+#   define S_IRGRP  0                           /* Read group */
+#   define S_IWGRP  0                           /* Write group */
+#   define S_IXGRP  0                           /* Execute group */
+#   define S_IROTH  0                           /* Read others */
+#   define S_IWOTH  0                           /* Write others */
+#   define S_IXOTH  0                           /* Execute others */
+#endif
+
+/* File type flags for d_type */
+#define DT_UNKNOWN  0
+#define DT_REG      S_IFREG
+#define DT_DIR      S_IFDIR
+#define DT_FIFO     S_IFIFO
+#define DT_SOCK     S_IFSOCK
+#define DT_CHR      S_IFCHR
+#define DT_BLK      S_IFBLK
+#define DT_LNK      S_IFLNK
+
+/* Macros for converting between st_mode and d_type */
+#define IFTODT(mode) ((mode) & S_IFMT)
+#define DTTOIF(type) (type)
+
+/*
+ * File type macros.  Note that block devices, sockets and links cannot be
+ * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
+ * only defined for compatibility.  These macros should always return false
+ * on Windows.
+ */
+#define	S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
+#define	S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
+#define	S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
+#define	S_ISLNK(mode)  (((mode) & S_IFMT) == S_IFLNK)
+#define	S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
+#define	S_ISCHR(mode)  (((mode) & S_IFMT) == S_IFCHR)
+#define	S_ISBLK(mode)  (((mode) & S_IFMT) == S_IFBLK)
+
+#else
+#include_next <sys/stat.h>
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * sys/time.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_SYS_TIME_H
+#define LIBCRYPTOCOMPAT_SYS_TIME_H
+
+#ifdef _MSC_VER
+#include <winsock2.h>
+int gettimeofday(struct timeval *tp, void *tzp);
+#else
+#include_next <sys/time.h>
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * sys/types.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_SYS_TYPES_H
+#define LIBCRYPTOCOMPAT_SYS_TYPES_H
+
+#ifdef _MSC_VER
+#include <../include/sys/types.h>
+#else
+#include_next <sys/types.h>
+#endif
+
+#include <stdint.h>
+
+#ifdef __MINGW32__
+#include <_bsd_types.h>
+#endif
+
+#ifdef _MSC_VER
+typedef unsigned char   u_char;
+typedef unsigned short  u_short;
+typedef unsigned int    u_int;
+
+#include <basetsd.h>
+typedef SSIZE_T ssize_t;
+
+#ifndef SSIZE_MAX
+#ifdef _WIN64
+#define SSIZE_MAX _I64_MAX
+#else
+#define SSIZE_MAX INT_MAX
+#endif
+#endif
+
+#endif
+
+#if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__bounded__)
+# define __bounded__(x, y, z)
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * sys/select.h compatibility shim
+ */
+
+#ifndef _WIN32
+#include_next <sys/uio.h>
+#else
+
+#include <sys/types.h>
+
+struct iovec {
+	void *iov_base;
+	size_t iov_len;
+};
+
+#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 @@
+/*
+ * Public domain
+ * sys/time.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_TIME_H
+#define LIBCRYPTOCOMPAT_TIME_H
+
+#ifdef _MSC_VER
+#include <../include/time.h>
+#define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL)
+#else
+#include_next <time.h>
+#endif
+
+#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 @@
+/*
+ * Public domain
+ * unistd.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_UNISTD_H
+#define LIBCRYPTOCOMPAT_UNISTD_H
+
+#ifndef _MSC_VER
+#include_next <unistd.h>
+#else
+
+#include <stdlib.h>
+#include <io.h>
+#include <process.h>
+
+#define R_OK    4
+#define W_OK    2
+#define X_OK    0
+#define F_OK    0
+
+#define access _access
+
+static inline unsigned int sleep(unsigned int seconds)
+{
+	Sleep(seconds * 1000);
+	return seconds;
+}
+
+#endif
+
+#ifndef HAVE_GETENTROPY
+int getentropy(void *buf, size_t buflen);
+#endif
+
+#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 @@
+/*
+ * Public domain
+ *
+ * BSD socket emulation code for Winsock2
+ * Brent Cook <bcook@openbsd.org>
+ */
+
+#ifndef LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H
+#define LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H
+
+#ifdef _WIN32
+
+#include <ws2tcpip.h>
+
+#define SHUT_RDWR SD_BOTH
+#define SHUT_RD   SD_RECEIVE
+#define SHUT_WR   SD_SEND
+
+#include <errno.h>
+#include <unistd.h>
+
+int posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
+
+int posix_close(int fd);
+ssize_t posix_read(int fd, void *buf, size_t count);
+
+ssize_t posix_write(int fd, const void *buf, size_t count);
+
+int posix_getsockopt(int sockfd, int level, int optname,
+	void *optval, socklen_t *optlen);
+
+int posix_setsockopt(int sockfd, int level, int optname,
+	const void *optval, socklen_t optlen);
+
+#ifndef NO_REDEF_POSIX_FUNCTIONS
+#define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen)
+#define close(fd) posix_close(fd)
+#define read(fd, buf, count) posix_read(fd, buf, count)
+#define write(fd, buf, count) posix_write(fd, buf, count)
+#define getsockopt(sockfd, level, optname, optval, optlen) \
+	posix_getsockopt(sockfd, level, optname, optval, optlen)
+#define setsockopt(sockfd, level, optname, optval, optlen) \
+	posix_setsockopt(sockfd, level, optname, optval, optlen)
+#endif
+
+#endif
+
+#endif
diff --git a/include/err.h b/include/err.h
deleted file mode 100644
index ec90327..0000000
--- a/include/err.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Public domain
- * err.h compatibility shim
- */
-
-#ifdef HAVE_ERR_H
-
-#include_next <err.h>
-
-#else
-
-#ifndef LIBCRYPTOCOMPAT_ERR_H
-#define LIBCRYPTOCOMPAT_ERR_H
-
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-
-#define err(exitcode, format, args...) \
-  errx(exitcode, format ": %s", ## args, strerror(errno))
-
-#define errx(exitcode, format, args...) \
-  do { warnx(format, ## args); exit(exitcode); } while (0)
-
-#define warn(format, args...) \
-  warnx(format ": %s", ## args, strerror(errno))
-
-#define warnx(format, args...) \
-  fprintf(stderr, format "\n", ## args)
-
-#endif
-
-#endif
diff --git a/include/machine/endian.h b/include/machine/endian.h
deleted file mode 100644
index 5ec39af..0000000
--- a/include/machine/endian.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Public domain
- * machine/endian.h compatibility shim
- */
-
-#ifndef LIBCRYPTOCOMPAT_BYTE_ORDER_H_
-#define LIBCRYPTOCOMPAT_BYTE_ORDER_H_
-
-#if defined(_WIN32)
-
-#define LITTLE_ENDIAN  1234
-#define BIG_ENDIAN 4321
-#define PDP_ENDIAN	3412
-
-/*
- * Use GCC and Visual Studio compiler defines to determine endian.
- */
-#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-#define BYTE_ORDER LITTLE_ENDIAN
-#else
-#define BYTE_ORDER BIG_ENDIAN
-#endif
-
-#elif defined(__linux__)
-#include <endian.h>
-
-#elif defined(__sun) || defined(_AIX) || defined(__hpux)
-#include <sys/types.h>
-#include <arpa/nameser_compat.h>
-
-#elif defined(__sgi)
-#include <standards.h>
-#include <sys/endian.h>
-
-#else
-#include_next <machine/endian.h>
-
-#endif
-
-#endif
diff --git a/include/netdb.h b/include/netdb.h
deleted file mode 100644
index d36b91d..0000000
--- a/include/netdb.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/*
- * Public domain
- * netdb.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <netdb.h>
-#else
-#include <win32netcompat.h>
-#endif
diff --git a/include/netinet/in.h b/include/netinet/in.h
deleted file mode 100644
index 20e57b8..0000000
--- a/include/netinet/in.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/*
- * Public domain
- * netinet/in.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <netinet/in.h>
-#else
-#include <win32netcompat.h>
-#endif
diff --git a/include/netinet/tcp.h b/include/netinet/tcp.h
deleted file mode 100644
index c98cf74..0000000
--- a/include/netinet/tcp.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/*
- * Public domain
- * netinet/tcp.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <netinet/tcp.h>
-#else
-#include <win32netcompat.h>
-#endif
diff --git a/include/poll.h b/include/poll.h
deleted file mode 100644
index c02a560..0000000
--- a/include/poll.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Public domain
- *
- * poll(2) emulation for Windows
- *
- * This emulates just-enough poll functionality on Windows to work in the
- * context of the openssl(1) program. This is not a replacement for
- * POSIX.1-2001 poll(2).
- *
- * Dongsheng Song <dongsheng.song@gmail.com>
- * Brent Cook <bcook@openbsd.org>
- */
-
-#ifndef LIBCRYPTOCOMPAT_POLL_H
-#define LIBCRYPTOCOMPAT_POLL_H
-
-#ifdef HAVE_POLL
-#include_next <poll.h>
-#else
-
-#include <winsock2.h>
-
-/* Type used for the number of file descriptors. */
-typedef unsigned long int nfds_t;
-
-#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600)
-/* Data structure describing a polling request. */
-struct pollfd {
-	int fd; /* file descriptor */
-	short events; /* requested events */
-	short revents; /* returned events */
-};
-
-/* Event types that can be polled */
-#define POLLIN 0x001 /* There is data to read. */
-#define POLLPRI 0x002 /* There is urgent data to read. */
-#define POLLOUT 0x004 /* Writing now will not block. */
-
-# define POLLRDNORM 0x040 /* Normal data may be read. */
-# define POLLRDBAND 0x080 /* Priority data may be read. */
-# define POLLWRNORM 0x100 /* Writing now will not block. */
-# define POLLWRBAND 0x200 /* Priority data may be written. */
-
-/* Event types always implicitly polled. */
-#define POLLERR 0x008 /* Error condition. */
-#define POLLHUP 0x010 /* Hung up. */
-#define POLLNVAL 0x020 /* Invalid polling request. */
-
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int poll(struct pollfd *pfds, nfds_t nfds, int timeout);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* HAVE_POLL */
-
-#endif /* LIBCRYPTOCOMPAT_POLL_H */
diff --git a/include/stdio.h b/include/stdio.h
deleted file mode 100644
index 76bd9da..0000000
--- a/include/stdio.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Public domain
- * stdio.h compatibility shim
- */
-
-#include_next <stdio.h>
-
-#ifndef LIBCRYPTOCOMPAT_STDIO_H
-#define LIBCRYPTOCOMPAT_STDIO_H
-
-#ifndef HAVE_ASPRINTF
-#include <stdarg.h>
-int vasprintf(char **str, const char *fmt, va_list ap);
-int asprintf(char **str, const char *fmt, ...);
-#endif
-
-#ifdef _WIN32
-
-void posix_perror(const char *s);
-FILE * posix_fopen(const char *path, const char *mode);
-int posix_rename(const char *oldpath, const char *newpath);
-
-#ifndef NO_REDEF_POSIX_FUNCTIONS
-#define perror(errnum) posix_perror(errnum)
-#define fopen(path, mode) posix_fopen(path, mode)
-#define rename(oldpath, newpath) posix_rename(oldpath, newpath)
-#endif
-
-#endif
-
-#endif
diff --git a/include/stdlib.h b/include/stdlib.h
deleted file mode 100644
index e77f0b4..0000000
--- a/include/stdlib.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * stdlib.h compatibility shim
- * Public domain
- */
-
-#include_next <stdlib.h>
-
-#ifndef LIBCRYPTOCOMPAT_STDLIB_H
-#define LIBCRYPTOCOMPAT_STDLIB_H
-
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <stdint.h>
-
-#ifndef HAVE_ARC4RANDOM_BUF
-uint32_t arc4random(void);
-void arc4random_buf(void *_buf, size_t n);
-#endif
-
-#ifndef HAVE_REALLOCARRAY
-void *reallocarray(void *, size_t, size_t);
-#endif
-
-#ifndef HAVE_STRTONUM
-long long strtonum(const char *nptr, long long minval,
-		long long maxval, const char **errstr);
-#endif
-
-#endif
diff --git a/include/string.h b/include/string.h
deleted file mode 100644
index 05d1ffc..0000000
--- a/include/string.h
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Public domain
- * string.h compatibility shim
- */
-
-#include_next <string.h>
-
-#ifndef LIBCRYPTOCOMPAT_STRING_H
-#define LIBCRYPTOCOMPAT_STRING_H
-
-#include <sys/types.h>
-
-#if defined(__sun) || defined(__hpux)
-/* Some functions historically defined in string.h were placed in strings.h by
- * SUS. Use the same hack as OS X and FreeBSD use to work around on Solaris and HPUX.
- */
-#include <strings.h>
-#endif
-
-#ifndef HAVE_STRLCPY
-size_t strlcpy(char *dst, const char *src, size_t siz);
-#endif
-
-#ifndef HAVE_STRLCAT
-size_t strlcat(char *dst, const char *src, size_t siz);
-#endif
-
-#ifndef HAVE_STRNDUP
-char * strndup(const char *str, size_t maxlen);
-/* the only user of strnlen is strndup, so only build it if needed */
-#ifndef HAVE_STRNLEN
-size_t strnlen(const char *str, size_t maxlen);
-#endif
-#endif
-
-#ifndef HAVE_STRSEP
-char *strsep(char **stringp, const char *delim);
-#endif
-
-#ifndef HAVE_EXPLICIT_BZERO
-void explicit_bzero(void *, size_t);
-#endif
-
-#ifndef HAVE_TIMINGSAFE_BCMP
-int timingsafe_bcmp(const void *b1, const void *b2, size_t n);
-#endif
-
-#ifndef HAVE_TIMINGSAFE_MEMCMP
-int timingsafe_memcmp(const void *b1, const void *b2, size_t len);
-#endif
-
-#ifndef HAVE_MEMMEM
-void * memmem(const void *big, size_t big_len, const void *little,
-	size_t little_len);
-#endif
-
-#ifdef _WIN32
-#include <errno.h>
-
-static inline char  *
-posix_strerror(int errnum)
-{
-	if (errnum == ECONNREFUSED) {
-		return "Connection refused";
-	}
-	return strerror(errnum);
-}
-
-#define strerror(errnum) posix_strerror(errnum)
-
-#endif
-
-#endif
diff --git a/include/sys/cdefs.h b/include/sys/cdefs.h
deleted file mode 100644
index 21ef031..0000000
--- a/include/sys/cdefs.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Public domain
- * sys/cdefs.h compatibility shim
- */
-
-#ifndef LIBCRYPTOCOMPAT_SYS_CDEFS_H
-#define LIBCRYPTOCOMPAT_SYS_CDEFS_H
-
-#ifdef _WIN32
-
-#define __warn_references(sym,msg)
-
-#else
-
-#include_next <sys/cdefs.h>
-
-#ifndef __warn_references
-
-#if defined(__GNUC__)  && defined (HAS_GNU_WARNING_LONG)
-#define __warn_references(sym,msg)          \
-  __asm__(".section .gnu.warning." __STRING(sym)  \
-         " ; .ascii \"" msg "\" ; .text");
-#else
-#define __warn_references(sym,msg)
-#endif
-
-#endif /* __warn_references */
-
-#endif /* _WIN32 */
-
-#endif /* LIBCRYPTOCOMPAT_SYS_CDEFS_H */
diff --git a/include/sys/ioctl.h b/include/sys/ioctl.h
deleted file mode 100644
index a255506..0000000
--- a/include/sys/ioctl.h
+++ /dev/null
@@ -1,11 +0,0 @@
-/*
- * Public domain
- * sys/ioctl.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <sys/ioctl.h>
-#else
-#include <win32netcompat.h>
-#define ioctl(fd, type, arg) ioctlsocket(fd, type, arg)
-#endif
diff --git a/include/sys/mman.h b/include/sys/mman.h
deleted file mode 100644
index d9eb6a9..0000000
--- a/include/sys/mman.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Public domain
- * sys/mman.h compatibility shim
- */
-
-#include_next <sys/mman.h>
-
-#ifndef LIBCRYPTOCOMPAT_MMAN_H
-#define LIBCRYPTOCOMPAT_MMAN_H
-
-#ifndef MAP_ANON
-#ifdef MAP_ANONYMOUS
-#define MAP_ANON MAP_ANONYMOUS
-#else
-#error "System does not support mapping anonymous pages?"
-#endif
-#endif
-
-#endif
diff --git a/include/sys/select.h b/include/sys/select.h
deleted file mode 100644
index 5ca0ea1..0000000
--- a/include/sys/select.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/*
- * Public domain
- * sys/select.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <sys/select.h>
-#else
-#include <win32netcompat.h>
-#endif
diff --git a/include/sys/socket.h b/include/sys/socket.h
deleted file mode 100644
index 17e84f1..0000000
--- a/include/sys/socket.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/*
- * Public domain
- * sys/socket.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <sys/socket.h>
-#else
-#include <win32netcompat.h>
-#endif
diff --git a/include/sys/times.h b/include/sys/times.h
deleted file mode 100644
index 5b9841b..0000000
--- a/include/sys/times.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/*
- * Public domain
- * sys/times.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <sys/times.h>
-#else
-#include <win32netcompat.h>
-#endif
diff --git a/include/sys/types.h b/include/sys/types.h
deleted file mode 100644
index bceedc2..0000000
--- a/include/sys/types.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Public domain
- * sys/types.h compatibility shim
- */
-
-#include_next <sys/types.h>
-
-#ifndef LIBCRYPTOCOMPAT_SYS_TYPES_H
-#define LIBCRYPTOCOMPAT_SYS_TYPES_H
-
-#include <stdint.h>
-
-#ifdef __MINGW32__
-#include <_bsd_types.h>
-#endif
-
-#if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__bounded__)
-# define __bounded__(x, y, z)
-#endif
-
-#endif
diff --git a/include/sys/uio.h b/include/sys/uio.h
deleted file mode 100644
index b4aee9e..0000000
--- a/include/sys/uio.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * Public domain
- * sys/select.h compatibility shim
- */
-
-#ifndef _WIN32
-#include_next <sys/uio.h>
-#else
-
-#include <sys/types.h>
-
-struct iovec {
-	void *iov_base;
-	size_t iov_len;
-};
-
-#endif
diff --git a/include/syslog.h b/include/syslog.h
deleted file mode 100644
index f61e33b..0000000
--- a/include/syslog.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Public domain
- * syslog.h compatibility shim
- */
-
-#ifndef LIBCRYPTOCOMPAT_SYSLOG_H
-#define LIBCRYPTOCOMPAT_SYSLOG_H
-
-#ifndef _WIN32
-#include_next <syslog.h>
-#else
-
-/* priorities */
-#define LOG_EMERG 0
-#define LOG_ALERT 1
-#define LOG_CRIT 2
-#define LOG_ERR 3
-#define LOG_WARNING 4
-#define LOG_NOTICE 5
-#define LOG_INFO 6
-#define LOG_DEBUG 7
-
-/* facility codes */
-#define LOG_KERN (0<<3)
-#define LOG_USER (1<<3)
-#define LOG_DAEMON (3<<3)
-
-/* flags for openlog */
-#define LOG_PID 0x01
-#define LOG_CONS 0x02
-
-extern void openlog(const char *ident, int option, int facility);
-extern void syslog(int priority, const char *fmt, ...)
-	__attribute__ ((__format__ (__printf__, 2, 3)));
-extern void closelog (void);
-#endif
-
-#endif /* LIBCRYPTOCOMPAT_SYSLOG_H */
diff --git a/include/unistd.h b/include/unistd.h
deleted file mode 100644
index 9b12034..0000000
--- a/include/unistd.h
+++ /dev/null
@@ -1,15 +0,0 @@
-/*
- * Public domain
- * unistd.h compatibility shim
- */
-
-#include_next <unistd.h>
-
-#ifndef LIBCRYPTOCOMPAT_UNISTD_H
-#define LIBCRYPTOCOMPAT_UNISTD_H
-
-#ifndef HAVE_GETENTROPY
-int getentropy(void *buf, size_t buflen);
-#endif
-
-#endif
diff --git a/include/win32netcompat.h b/include/win32netcompat.h
deleted file mode 100644
index 452cfba..0000000
--- a/include/win32netcompat.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Public domain
- *
- * BSD socket emulation code for Winsock2
- * Brent Cook <bcook@openbsd.org>
- */
-
-#ifndef LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H
-#define LIBCRYPTOCOMPAT_WIN32NETCOMPAT_H
-
-#ifdef _WIN32
-
-#include <ws2tcpip.h>
-
-#define SHUT_RDWR SD_BOTH
-#define SHUT_RD   SD_RECEIVE
-#define SHUT_WR   SD_SEND
-
-#include <errno.h>
-#include <unistd.h>
-
-int posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
-
-int posix_close(int fd);
-ssize_t posix_read(int fd, void *buf, size_t count);
-
-ssize_t posix_write(int fd, const void *buf, size_t count);
-
-int posix_getsockopt(int sockfd, int level, int optname,
-	void *optval, socklen_t *optlen);
-
-int posix_setsockopt(int sockfd, int level, int optname,
-	const void *optval, socklen_t optlen);
-
-#ifndef NO_REDEF_POSIX_FUNCTIONS
-#define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen)
-#define close(fd) posix_close(fd)
-#define read(fd, buf, count) posix_read(fd, buf, count)
-#define write(fd, buf, count) posix_write(fd, buf, count)
-#define getsockopt(sockfd, level, optname, optval, optlen) \
-	posix_getsockopt(sockfd, level, optname, optval, optlen)
-#define setsockopt(sockfd, level, optname, optval, optlen) \
-	posix_setsockopt(sockfd, level, optname, optval, optlen)
-#endif
-
-#endif
-
-#endif
diff --git a/libtls-standalone/include/string.h b/libtls-standalone/include/string.h
index 05d1ffc..eabc4c4 100644
--- a/libtls-standalone/include/string.h
+++ b/libtls-standalone/include/string.h
@@ -3,11 +3,15 @@
  * string.h compatibility shim
  */
 
-#include_next <string.h>
-
 #ifndef LIBCRYPTOCOMPAT_STRING_H
 #define LIBCRYPTOCOMPAT_STRING_H
 
+#ifdef _MSC_VER
+#include <../include/string.h>
+#else
+#include_next <string.h>
+#endif
+
 #include <sys/types.h>
 
 #if defined(__sun) || defined(__hpux)
@@ -17,6 +21,11 @@
 #include <strings.h>
 #endif
 
+#ifndef HAVE_STRCASECMP
+int strcasecmp(const char *s1, const char *s2);
+int strncasecmp(const char *s1, const char *s2, size_t len);
+#endif
+
 #ifndef HAVE_STRLCPY
 size_t strlcpy(char *dst, const char *src, size_t siz);
 #endif
diff --git a/m4/check-os-options.m4 b/m4/check-os-options.m4
index 4a7aec8..d1695f6 100644
--- a/m4/check-os-options.m4
+++ b/m4/check-os-options.m4
@@ -51,7 +51,7 @@ case $host_os in
 		CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE -D_POSIX -D_POSIX_SOURCE -D__USE_MINGW_ANSI_STDIO"
 		CPPFLAGS="$CPPFLAGS -D_REENTRANT -D_POSIX_THREAD_SAFE_FUNCTIONS"
 		CPPFLAGS="$CPPFLAGS -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0501"
-		CPPFLAGS="$CPPFLAGS -DOPENSSL_NO_SPEED -DNO_SYSLOG"
+		CPPFLAGS="$CPPFLAGS -DOPENSSL_NO_SPEED"
 		CFLAGS="$CFLAGS -static-libgcc"
 		LDFLAGS="$LDFLAGS -static-libgcc"
 		AC_SUBST([PLATFORM_LDADD], ['-lws2_32'])
diff --git a/patches/arc4random.c.patch b/patches/arc4random.c.patch
new file mode 100644
index 0000000..9f9c476
--- /dev/null
+++ b/patches/arc4random.c.patch
@@ -0,0 +1,15 @@
+--- crypto/compat/arc4random.c.orig	2015-07-20 07:41:17.000000000 -0600
++++ crypto/compat/arc4random.c	2015-07-20 07:41:58.000000000 -0600
+@@ -36,8 +36,11 @@
+ #define KEYSTREAM_ONLY
+ #include "chacha_private.h"
+ 
++#ifndef min
+ #define min(a, b) ((a) < (b) ? (a) : (b))
+-#ifdef __GNUC__
++#endif
++
++#if defined(__GNUC__) || defined(_MSC_VER)
+ #define inline __inline
+ #else				/* !__GNUC__ */
+ #define inline
diff --git a/patches/openssl.c.patch b/patches/openssl.c.patch
index c086ba5..275e9ea 100644
--- a/patches/openssl.c.patch
+++ b/patches/openssl.c.patch
@@ -1,10 +1,11 @@
---- apps/openssl.c.orig	2015-06-05 03:42:12.956112944 -0500
-+++ apps/openssl.c	2015-06-05 03:41:54.215381908 -0500
-@@ -130,6 +130,18 @@
+--- apps/openssl.c.orig	2015-07-20 02:01:42.000000000 -0600
++++ apps/openssl.c	2015-07-20 02:02:00.000000000 -0600
+@@ -130,6 +130,19 @@
  #include <openssl/engine.h>
  #endif
- 
+
 +#ifdef _WIN32
++#include <io.h>
 +#include <fcntl.h>
 +static void set_stdio_binary(void)
 +{
@@ -18,12 +19,22 @@
 +
  #include "progs.h"
  #include "s_apps.h"
- 
-@@ -216,6 +228,7 @@
+
+@@ -204,7 +216,9 @@
+ static void
+ openssl_startup(void)
+ {
++#ifndef _WIN32
+ 	signal(SIGPIPE, SIG_IGN);
++#endif
+
+ 	CRYPTO_malloc_init();
+ 	OpenSSL_add_all_algorithms();
+@@ -216,6 +230,7 @@
  #endif
- 
+
  	setup_ui_method();
 +	set_stdio_binary();
  }
- 
+
  static void
diff --git a/patches/opensslconf.h.patch b/patches/opensslconf.h.patch
new file mode 100644
index 0000000..0507c92
--- /dev/null
+++ b/patches/opensslconf.h.patch
@@ -0,0 +1,13 @@
+--- include/openssl/opensslconf.h.orig	2015-07-19 23:21:47.000000000 -0600
++++ include/openssl/opensslconf.h	2015-07-19 23:21:17.000000000 -0600
+@@ -1,6 +1,10 @@
+ #include <openssl/opensslfeatures.h>
+ /* crypto/opensslconf.h.in */
+
++#if defined(_MSC_VER) && !defined(__attribute__)
++#define __attribute__(a)
++#endif
++
+ /* Generate 80386 code? */
+ #undef I386_ONLY
+
diff --git a/patches/ossl_typ.h.patch b/patches/ossl_typ.h.patch
index af9419a..f213347 100644
--- a/patches/ossl_typ.h.patch
+++ b/patches/ossl_typ.h.patch
@@ -1,15 +1,17 @@
 --- include/openssl/ossl_typ.h.orig	2015-07-06 13:21:18.788571423 -0700
 +++ include/openssl/ossl_typ.h	2015-07-06 13:24:14.906468003 -0700
-@@ -100,6 +100,20 @@
+@@ -100,6 +100,22 @@
  typedef struct ASN1_ITEM_st ASN1_ITEM;
  typedef struct asn1_pctx_st ASN1_PCTX;
 
 +#if defined(_WIN32) && defined(__WINCRYPT_H__)
++#ifndef LIBRESSL_INTERNAL
 +#ifdef _MSC_VER
 +#pragma message("Warning, overriding WinCrypt defines")
 +#else
 +#warning overriding WinCrypt defines
 +#endif
++#endif
 +#undef X509_NAME
 +#undef X509_CERT_PAIR
 +#undef X509_EXTENSIONS
diff --git a/patches/pkcs7.h.patch b/patches/pkcs7.h.patch
index fa8b297..4c6f6ba 100644
--- a/patches/pkcs7.h.patch
+++ b/patches/pkcs7.h.patch
@@ -1,15 +1,17 @@
 --- include/openssl/pkcs7.h.orig	2015-07-06 13:26:27.369203527 -0700
 +++ include/openssl/pkcs7.h	2015-07-06 13:27:37.637051967 -0700
-@@ -69,6 +69,16 @@
+@@ -69,6 +69,18 @@
  extern "C" {
  #endif
 
 +#if defined(_WIN32) && defined(__WINCRYPT_H__)
++#ifndef LIBRESSL_INTERNAL
 +#ifdef _MSC_VER
 +#pragma message("Warning, overriding WinCrypt defines")
 +#else
 +#warning overriding WinCrypt defines
 +#endif
++#endif
 +#undef PKCS7_ISSUER_AND_SERIAL
 +#undef PKCS7_SIGNER_INFO
 +#endif
diff --git a/patches/x509.h.patch b/patches/x509.h.patch
index 13686c1..f0e59b2 100644
--- a/patches/x509.h.patch
+++ b/patches/x509.h.patch
@@ -1,15 +1,17 @@
 --- include/openssl/x509.h.orig	2015-07-06 13:15:15.059306046 -0700
 +++ include/openssl/x509.h	2015-07-06 13:16:10.506118278 -0700
-@@ -112,6 +112,17 @@
+@@ -112,6 +112,19 @@
  extern "C" {
  #endif
 
-+#if defined(_WIN32) && defined(__WINCRYPT_H__)
++#if defined(_WIN32)
++#ifndef LIBRESSL_INTERNAL
 +#ifdef _MSC_VER
 +#pragma message("Warning, overriding WinCrypt defines")
 +#else
 +#warning overriding WinCrypt defines
 +#endif
++#endif
 +#undef X509_NAME
 +#undef X509_CERT_PAIR
 +#undef X509_EXTENSIONS
diff --git a/ssl/CMakeLists.txt b/ssl/CMakeLists.txt
new file mode 100644
index 0000000..c26f2b4
--- /dev/null
+++ b/ssl/CMakeLists.txt
@@ -0,0 +1,53 @@
+include_directories(
+	.
+	../include
+	../include/compat
+)
+
+add_library(
+	ssl
+
+	bio_ssl.c
+	bs_ber.c
+	bs_cbb.c
+	bs_cbs.c
+	d1_both.c
+	d1_clnt.c
+	d1_enc.c
+	d1_lib.c
+	d1_meth.c
+	d1_pkt.c
+	d1_srtp.c
+	d1_srvr.c
+	pqueue.c
+	s23_clnt.c
+	s23_lib.c
+	s23_meth.c
+	s23_pkt.c
+	s23_srvr.c
+	s3_both.c
+	s3_cbc.c
+	s3_clnt.c
+	s3_enc.c
+	s3_lib.c
+	s3_meth.c
+	s3_pkt.c
+	s3_srvr.c
+	ssl_algs.c
+	ssl_asn1.c
+	ssl_cert.c
+	ssl_ciph.c
+	ssl_err.c
+	ssl_err2.c
+	ssl_lib.c
+	ssl_rsa.c
+	ssl_sess.c
+	ssl_stat.c
+	ssl_txt.c
+	t1_clnt.c
+	t1_enc.c
+	t1_lib.c
+	t1_meth.c
+	t1_reneg.c
+	t1_srvr.c
+)
diff --git a/ssl/Makefile.am b/ssl/Makefile.am
index db60775..6c36cf2 100644
--- a/ssl/Makefile.am
+++ b/ssl/Makefile.am
@@ -3,6 +3,7 @@ include $(top_srcdir)/Makefile.am.common
 lib_LTLIBRARIES = libssl.la
 
 EXTRA_DIST = VERSION
+EXTRA_DIST += CMakeLists.txt
 
 libssl_la_LDFLAGS = -version-info @LIBSSL_VERSION@ -no-undefined
 libssl_la_LIBADD = ../crypto/libcrypto.la
diff --git a/tls/CMakeLists.txt b/tls/CMakeLists.txt
new file mode 100644
index 0000000..3e61844
--- /dev/null
+++ b/tls/CMakeLists.txt
@@ -0,0 +1,22 @@
+include_directories(
+	.
+	../include
+	../include/compat
+)
+
+set(
+	TLS_SRC
+	tls.c
+	tls_client.c
+	tls_config.c
+	tls_server.c
+	tls_util.c
+	tls_verify.c
+)
+
+
+if(NOT HAVE_STRCASECMP)
+	set(TLS_SRC ${TLS_SRC} strsep.c)
+endif()
+
+add_library(tls ${TLS_SRC})
diff --git a/tls/Makefile.am b/tls/Makefile.am
index e8c4713..82ec211 100644
--- a/tls/Makefile.am
+++ b/tls/Makefile.am
@@ -3,6 +3,7 @@ include $(top_srcdir)/Makefile.am.common
 lib_LTLIBRARIES = libtls.la
 
 EXTRA_DIST = VERSION
+EXTRA_DIST += CMakeLists.txt
 
 libtls_la_LDFLAGS = -version-info @LIBTLS_VERSION@ -no-undefined
 libtls_la_LIBADD = ../crypto/libcrypto.la ../ssl/libssl.la $(PLATFORM_LDADD)
diff --git a/update.sh b/update.sh
index 9b6d7c8..91e7abb 100755
--- a/update.sh
+++ b/update.sh
@@ -70,6 +70,7 @@ for i in crypto/compat libtls-standalone/compat; do
 		$libc_src/crypt/chacha_private.h \
 		$libc_src/string/explicit_bzero.c \
 		$libc_src/stdlib/reallocarray.c \
+		$libc_src/string/strcasecmp.c \
 		$libc_src/string/strlcpy.c \
 		$libc_src/string/strlcat.c \
 		$libc_src/string/strndup.c \
@@ -81,9 +82,9 @@ for i in crypto/compat libtls-standalone/compat; do
 		$i
 done
 
-$CP include/stdlib.h \
-	include/string.h \
-	include/unistd.h \
+$CP include/compat/stdlib.h \
+	include/compat/string.h \
+	include/compat/unistd.h \
 	libtls-standalone/include
 
 $CP crypto/compat/arc4random*.h \
@@ -210,10 +211,6 @@ for i in `awk '/SOURCES|HEADERS/ { print $3 }' apps/Makefile.am` ; do
 		$CP $openssl_app_src/$i apps
 	fi
 done
-patch -p0 < patches/openssl.c.patch
-patch -p0 < patches/ossl_typ.h.patch
-patch -p0 < patches/pkcs7.h.patch
-patch -p0 < patches/x509.h.patch
 
 # copy libssl source
 echo "copying libssl source"
@@ -276,6 +273,11 @@ add_man_links() {
 	done
 }
 
+# apply local patches (Windows support)
+for i in patches/*.patch; do
+    patch -p0 < $i
+done
+
 # copy manpages
 echo "copying manpages"
 echo dist_man_MANS= > man/Makefile.am
-- 
cgit v1.2.3-55-g6feb