diff options
Diffstat (limited to 'src/libmowgli/platform')
-rw-r--r-- | src/libmowgli/platform/Makefile | 9 | ||||
-rw-r--r-- | src/libmowgli/platform/autoconf.h.in | 105 | ||||
-rw-r--r-- | src/libmowgli/platform/constructor.h | 54 | ||||
-rw-r--r-- | src/libmowgli/platform/machine.h | 334 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/Makefile | 15 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/fork.c | 166 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/gettimeofday.c | 66 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/inet.c | 80 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/pipe.c | 31 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/setenv.c | 31 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/socketpair.c | 109 | ||||
-rw-r--r-- | src/libmowgli/platform/win32/win32_stdinc.h | 62 |
12 files changed, 1062 insertions, 0 deletions
diff --git a/src/libmowgli/platform/Makefile b/src/libmowgli/platform/Makefile new file mode 100644 index 0000000..6b6d7b9 --- /dev/null +++ b/src/libmowgli/platform/Makefile @@ -0,0 +1,9 @@ +SUBDIRS = win32 + +INCLUDES = constructor.h machine.h + +include ../../../buildsys.mk + +includesubdir = $(PACKAGE_NAME)/platform + +include ../../../extra.mk diff --git a/src/libmowgli/platform/autoconf.h.in b/src/libmowgli/platform/autoconf.h.in new file mode 100644 index 0000000..fb77f69 --- /dev/null +++ b/src/libmowgli/platform/autoconf.h.in @@ -0,0 +1,105 @@ +/* src/libmowgli/platform/autoconf.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the `dispatch_block' function. */ +#undef HAVE_DISPATCH_BLOCK + +/* Define to 1 if you have the `fcntl' function. */ +#undef HAVE_FCNTL + +/* Define to 1 if you have the <inttypes.h> header file. */ +#undef HAVE_INTTYPES_H + +/* Define to 1 if you have the `kqueue' function. */ +#undef HAVE_KQUEUE + +/* Define to 1 if you have the <memory.h> header file. */ +#undef HAVE_MEMORY_H + +/* Define to 1 if you have the `mmap' function. */ +#undef HAVE_MMAP + +/* Define to 1 if OpenSSL is available */ +#undef HAVE_OPENSSL + +/* Define to 1 if you have the <poll.h> header file. */ +#undef HAVE_POLL_H + +/* Define to 1 if you have the `port_create' function. */ +#undef HAVE_PORT_CREATE + +/* Define to 1 if you have the `pstat' function. */ +#undef HAVE_PSTAT + +/* Define to 1 if the PS_STRINGS struct exists on your platform (likely no). + */ +#undef HAVE_PS_STRINGS + +/* Define if you have POSIX threads libraries and header files. */ +#undef HAVE_PTHREAD + +/* Define to 1 if you have the `select' function. */ +#undef HAVE_SELECT + +/* Define to 1 if you have the `setproctitle' function. */ +#undef HAVE_SETPROCTITLE + +/* Define to 1 if you have the <stdint.h> header file. */ +#undef HAVE_STDINT_H + +/* Define to 1 if you have the <stdlib.h> header file. */ +#undef HAVE_STDLIB_H + +/* Define to 1 if you have the <strings.h> header file. */ +#undef HAVE_STRINGS_H + +/* Define to 1 if you have the <string.h> header file. */ +#undef HAVE_STRING_H + +/* Define to 1 if you have the <sys/epoll.h> header file. */ +#undef HAVE_SYS_EPOLL_H + +/* Define to 1 if you have the <sys/prctl.h> header file. */ +#undef HAVE_SYS_PRCTL_H + +/* Define to 1 if you have the <sys/pstat.h> header file. */ +#undef HAVE_SYS_PSTAT_H + +/* Define to 1 if you have the <sys/select.h> header file. */ +#undef HAVE_SYS_SELECT_H + +/* Define to 1 if you have the <sys/stat.h> header file. */ +#undef HAVE_SYS_STAT_H + +/* Define to 1 if you have the <sys/types.h> header file. */ +#undef HAVE_SYS_TYPES_H + +/* Define to 1 if you have the <unistd.h> header file. */ +#undef HAVE_UNISTD_H + +/* Define to 1 if you have the <winsock2.h> header file. */ +#undef HAVE_WINSOCK2_H + +/* Define to the address where bug reports for this package should be sent. */ +#undef PACKAGE_BUGREPORT + +/* Define to the full name of this package. */ +#undef PACKAGE_NAME + +/* Define to the full name and version of this package. */ +#undef PACKAGE_STRING + +/* Define to the one symbol short name of this package. */ +#undef PACKAGE_TARNAME + +/* Define to the home page for this package. */ +#undef PACKAGE_URL + +/* Define to the version of this package. */ +#undef PACKAGE_VERSION + +/* Define to necessary symbol if this constant uses a non-standard name on + your system. */ +#undef PTHREAD_CREATE_JOINABLE + +/* Define to 1 if you have the ANSI C header files. */ +#undef STDC_HEADERS diff --git a/src/libmowgli/platform/constructor.h b/src/libmowgli/platform/constructor.h new file mode 100644 index 0000000..8446f44 --- /dev/null +++ b/src/libmowgli/platform/constructor.h @@ -0,0 +1,54 @@ +/* + * constructor.h + * Code for setting up automatic initializer functions portably. + * + * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __MOWGLI_PLATFORM_CONSTRUCTOR_H__ +#define __MOWGLI_PLATFORM_CONSTRUCTOR_H__ + +#ifdef _MSC_VER + +/* + * Automatic constructors are not yet officially supported in MSVC, however, + * there is a similar feature where functions in the ".CRT$XCU" section are + * evaluated prior to DllMain(), main() and friends. + * + * See http://blogs.msdn.com/b/vcblog/archive/2006/10/20/crt-initialization.aspx + * for more information. + */ +#define MOWGLI_BOOTSTRAP_FUNC(func) \ + static void __cdecl func(void); \ + __declspec(allocate(".CRT$XCU")) void (__cdecl *func##_)(void) = func; \ + static void __cdecl func(void) + +#elif defined(__GNUC__) || defined(__SUNPRO_C) + +#define MOWGLI_BOOTSTRAP_FUNC(func) \ + static void func(void) __attribute__((constructor)); \ + static void func(void) + +#else + +#error MOWGLI_BOOTSTRAP_FUNC not implemented for your platform :( + +#endif + +#endif diff --git a/src/libmowgli/platform/machine.h b/src/libmowgli/platform/machine.h new file mode 100644 index 0000000..b2a2e50 --- /dev/null +++ b/src/libmowgli/platform/machine.h @@ -0,0 +1,334 @@ +/* + * libmowgli: A collection of useful routines for programming. + * machine.h: Defines to discover what machine we're on easily + * + * Copyright (c) 2012 Patrick McFarland <pmcfarland@adterrasperaspera.com> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mowgli.h" + +/* Machine environment specific macros, mostly sourced from this URL: + * http://sourceforge.net/apps/mediawiki/predef/ + * + * Please note: Just because a compiler, CPU, or OS is listed in this file, + * that doesn't mean that it is supported by libmowgli. libmowgli requires + * a 32-bit or higher CPU and an OS that supports a supported mutex and + * thread scheme (if you intend on having functional multithreaded + * operation). + */ + +#ifndef __MOWGLI_MACHINE_H__ +#define __MOWGLI_MACHINE_H__ + +#if defined __clang__ +#define MOWGLI_COMPILER_CLANG +#define MOWGLI_COMPILER clang +#elif defined __INTEL_COMPILER || defined __ICC || defined __ICL +#define MOWGLI_COMPILER_ICC +#define MOWGLI_COMPILER icc +#elif defined __CC_ARM +#define MOWGLI_COMPILER_ARM +#define MOWGLI_COMPILER arm +#elif defined __xlc__ || defined __xlC__ +#define MOWGLI_COMPILER_IBM +#define MOWGLI_COMPILER ibm +#elif defined __SUNPRO_C || defined __SUNPRO_CC +#define MOWGLI_COMPILER_SUN +#define MOWGLI_COMPILER sun +#elif defined __GNUC__ +#define MOWGLI_COMPILER_GCC +#define MOWGLI_COMPILER gcc +#elif defined _MSC_VER +#define MOWGLI_COMPILER_MSVC +#define MOWGLI_COMPILER msvc +#else +#define MOWGLI_COMPILER_UNKNOWN +#define MOWGLI_COMPILER unknown +#endif + +#if defined __GNUC__ +#define MOWGLI_COMPILER_GCC_COMPAT +#endif + +#if defined __amd64__ || defined __amd64 || defined __x86_64__ || defined __x86_64 || defined _M_X64 || defined _M_AMD64 +#define MOWGLI_CPU_X86_64 +#define MOWGLI_CPU x86_64 +#define MOWGLI_CPU_BITS_64 +#define MOWGLI_CPU_BITS 64 +#define MOWGLI_CPU_ENDIAN_LITTLE +#define MOWGLI_CPU_ENDIAN little +#elif defined __i386__ || defined __i386 || defined __IA32__ || defined _M_IX86 || defined __X86__ || defined _X86__ || defined __I86__ +#define MOWGLI_CPU_X86 +#define MOWGLI_CPU x86 +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +#define MOWGLI_CPU_ENDIAN_LITTLE +#define MOWGLI_CPU_ENDIAN little +#elif defined __arm__ || defined __TARGET_ARCH_ARM || defined _ARM +#if defined __thumb__ || defined __TARGET_ARCH_THUMB +#define MOWGLI_CPU_ARM_THUMB +#endif +#define MOWGLI_CPU_ARM +#define MOWGLI_CPU arm +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +// ARM can be either endian +#elif defined __hppa__ || defined __HPPA__ || defined __hppa +#if defined _PA_RISC2_0 || defined __HPPA20__ || defined __RISC2_0__ +#define MOWGLI_CPU_HPPA20 +#define MOWGLI_CPU hppa20 +#define MOWGLI_CPU_BITS_64 +#define MOWGLI_CPU_BITS 64 +#else +#define MOWGLI_CPU_HPPA10 +#define MOWGLI_CPU hppa10 +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +#endif +#define MOWGLI_CPU_ENDIAN_BIG +#define MOWGLI_CPU_ENDIAN big +#elif defined __ia64__ || defined __IA64__ || defined _M_IA64 || defined __ia64 || defined __itanium__ +#define MOWGLI_CPU_ITANIUM +#define MOWGLI_CPU itanium +#define MOWGLI_CPU_BITS_64 +#define MOWGLI_CPU_BITS 64 +// Itanium can be either endian +#elif defined __mips__ || defined mips || defined __mips || defined __MIPS__ +#if defined __mips64 || defined __mips64__ +#define MOWGLI_CPU_MIPS64 +#define MOWGLI_CPU mips64 +#define MOWGLI_CPU_BITS_64 +#define MOWGLI_CPU_BITS 64 +#else +#define MOWGLI_CPU_MIPS +#define MOWGLI_CPU mips +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +#endif +// MIPS can be either endian +#elif defined __powerpc || defined __powerpc__ || defined __POWERPC__ || defined __ppc__ || defined _M_PPC +#if defined __ppc64__ || defined __PPC64__ +#define MOWGLI_CPU_POWERPC64 +#define MOWGLI_CPU powerpc64 +#define MOWGLI_CPU_BITS_64 +#define MOWGLI_CPU_BITS 64 +#else +#define MOWGLI_CPU powerpc +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +#endif +#define MOWGLI_CPU_POWERPC +// PowerPC can be either endian +#elif defined __sparc__ || defined __sparc +#if defined __sparcv9 || defined __sparc64 || defined __sparc64__ +#define MOWGLI_CPU_SPARC64 +#define MOWGLI_CPU sparc64 +#define MOWGLI_CPU_BITS_64 +#define MOWGLI_CPU_BITS 64 +#else +#define MOWGLI_CPU_SPARC +#define MOWGLI_CPU sparc +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +#endif +#define MOWGLI_CPU_ENDIAN_BIG +#define MOWGLI_CPU_ENDIAN big +#elif defined __alpha || defined __alpha__ || defined _M_ALPHA +#define MOWGLI_CPU_ALPHA +#define MOWGLI_CPU alpha +#define MOWGLI_CPU_BITS_64 +#define MOWGLI_CPU_BITS 64 +#define MOWGLI_CPU_ENDIAN_LITTLE +#define MOWGLI_CPU_ENDIAN little +#elif defined __avr32__ || defined __AVR32__ +#define MOWGLI_CPU_AVR32 +#define MOWGLI_CPU avr +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +#define MOWGLI_CPU_ENDIAN_LITTLE +#define MOWGLI_CPU_ENDIAN little +#elif defined __sh__ || defined __SH__ +#define MOWGLI_CPU_SuperH +#define MOWGLI_CPU superh +#define MOWGLI_CPU_BITS_32 +#define MOWGLI_CPU_BITS 32 +// SyoerH can be either endian +#endif + +#ifndef MOWGLI_CPU +#define MOWGLI_CPU_UNKNOWN +#define MOWGLI_CPU unknown +#endif + +#ifndef MOWGLI_CPU_BITS +#if defined _LP64 || defined __LP64 +#define MOWGLI_CPU_BITS 64 +#define MOWGLI_CPU_BITS_64 +#elif +#define MOWGLI_CPU_BITS 32 +#define MOWGLI_CPU_BITS_32 +#endif +#endif + +#if defined __linux || defined __linux__ +#define MOWGLI_OS_LINUX +#define MOWGLI_OS linux +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#elif defined __APPLE__ +#define MOWGLI_OS_OSX +#define MOWGLI_OS osx +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#define MOWGLI_OS_BSD_TYPE + +#ifdef MOWGLI_CPU_POWERPC +#define MOWGLI_CPU_ENDIAN_BIG +#define MOWGLI_CPU_ENDIAN big +#endif +#elif defined __WINDOWS__ || defined _WIN32 || defined __WIN32__ || defined __TOS_WIN__ +#if defined _WIN64 +#define MOWGLI_OS_WIN64 +#define MOWGLI_OS win64 +#else +#define MOWGLI_OS_WIN32 +#define MOWGLI_OS win32 +#endif +#define MOWGLI_OS_WIN +#define MOWGLI_OS_THREADS_WIN +#define MOWGLI_OS_THREADS win +#define MOWGLI_OS_MUTEX_WIN +#define MOWGLI_OS_MUTEX win + +#if defined __CYGWIN__ +#define MOWGLI_OS_WIN_CYGWIN +#elif defined __MINGW32__ +#define MOWGLI_OS_WIN_MINGW +#endif + +#if defined MOWGLI_CPU_POWERPC || defined MOWGLI_CPU_MIPS || defined MOWGLI_CPU_ITANIUM +#define MOWGLI_ENDIAN_CPU_LITTLE +#endif +#elif defined __NetBSD__ || defined __OpenBSD__ || defined __FreeBSD__ || defined __bsdi__ || defined __DragonFly__ || defined BSD || defined _SYSTYPE_BSD +#define MOWGLI_OS_BSD +#define MOWGLI_OS bsd +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#define MOWGLI_OS_BSD_TYPE +#elif defined __GNU__ +#define MOWGLI_OS_HURD +#define MOWGLI_OS hurd +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#elif defined sco || defined __sco +#define MOWGLI_OS_SCO +#define MOWGLI_OS sco +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#define MOWGLI_OS_UNIX_TYPE +#elif defined sun || defined __sun +#define MOWGLI_OS_SOLARIS +#define MOWGLI_OS solaris +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#define MOWGLI_OS_UNIX_TYPE +#if defined MOWGLI_CPU_POWERPC +#define MOWGLI_CPU_ENDIAN_LITTLE +#define MOWGLI_CPU_ENDIAN little +#endif +#elif defined _hpux || defined hpux || defined __hpux +#define MOWGLI_OS_HPUX +#define MOWGLI_OS hpux +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#define MOWGLI_OS_UNIX_TYPE +#if defined MOWGLI_CPU_ITANIUM +#define MOWGLI_CPU_ENDIAN_BIG +#define MOWGLI_CPU_ENDIAN big +#endif +#elif defined __QNX__ || defined __QNXNTO__ +#define MOWGLI_OS_QNX +#define MOWGLI_OS qnx +#define MOWGLI_OS_THREADS_QNX +#define MOWGLI_OS_THREADS qnx +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#elif defined __vms || defined __VMS +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#if defined MOWGLI_CPU_ITANIUM +#define MOWGLI_CPU_ENDIAN_LITTLE +#define MOWGLI_CPU_ENDIAN little +#endif +#endif + +#ifndef MOWGLI_OS +#if defined __unix__ || __unix || unix +#define MOWGLI_OS_UNIX +#define MOWGLI_OS unix +#define MOWGLI_OS_THREADS_POSIX +#define MOWGLI_OS_THREADS posix +#define MOWGLI_OS_MUTEX_POSIX +#define MOWGLI_OS_MUTEX posix +#define MOWGLI_OS_UNIX_TYPE +#endif +#define MOWGLI_OS_UNKNOWN +#define MOWGLI_OS unknown +#warning OS unsupported/unknown +#endif + +#ifndef MOWGLI_OS_THREADS +#define MOWGLI_OS_THREADS_NULL +#define MOWGLI_OS_THREADS null +#endif + +#ifndef MOWGLI_OS_MUTEX +#define MOWGLI_OS_MUTEX_NULL +#define MOWGLI_OS_MUTEX null +#endif + +#ifndef MOWGLI_CPU_ENDIAN +#if defined __BIG_ENDIAN__ || defined __ARMEB__ || defined __THUMBEB__ || defined _MIPSEB || defined __MIPSEB || defined __MIPSEB__ || __BYTE_ORDER == __BIG_ENDIAN +#define MOWGLI_CPU_ENDIAN_BIG +#define MOWGLI_CPU_ENDIAN big +#elif defined __LITTLE_ENDIAN__ || defined __ARMEL__ || defined __THUMBEL__ || defined _MIPSEL || defined __MIPSEL || defined __MIPSEL__ || __BYTE_ORDER == __LITTLE_ENDIAN +#define MOWGLI_CPU_ENDIAN_LITTLE +#define MOWGLI_CPU_ENDIAN little +#else +#warning CPU endianness unknown, some functions of libmowgli will not work +#endif +#endif + +#endif diff --git a/src/libmowgli/platform/win32/Makefile b/src/libmowgli/platform/win32/Makefile new file mode 100644 index 0000000..653bf2d --- /dev/null +++ b/src/libmowgli/platform/win32/Makefile @@ -0,0 +1,15 @@ +include ../../../../extra.mk + +STATIC_PIC_LIB_NOINST = ${LIBMOWGLI_SHARED_PLATFORM_WIN32} +STATIC_LIB_NOINST = ${LIBMOWGLI_STATIC_PLATFORM_WIN32} + +SRCS = fork.c gettimeofday.c inet.c pipe.c socketpair.c setenv.c + +INCLUDES = win32_stdinc.h + +include ../../../../buildsys.mk + +includesubdir = $(PACKAGE_NAME)/platform/win32 + +CPPFLAGS += -I. -I.. -I../.. -I../../../.. -DMOWGLI_CORE + diff --git a/src/libmowgli/platform/win32/fork.c b/src/libmowgli/platform/win32/fork.c new file mode 100644 index 0000000..c644122 --- /dev/null +++ b/src/libmowgli/platform/win32/fork.c @@ -0,0 +1,166 @@ +/* + * libmowgli: A collection of useful routines for programming. + * fork.c: Fastest possible NT fork() implementation + * + * Copyright (c) 2012 TortoiseLabs, LLC. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * References: + * - undocumented.ntinternals.net + * - http://www.cs.miami.edu/~burt/journal/NT/processinit.html + */ + +#include "mowgli.h" + +#ifdef NOTYET + +#ifdef _WIN32 + +#ifndef _WIN32_WINNT + +int fork(void) +{ +#warning fork is not possible on your platform in any sane way, sorry :( + return -ENOSYS; +} + +#else + +extern NTSTATUS NTAPI CsrCallClientServer(void *message, void *userdata, uint32_t opcode, uint32_t size); + +/* + * Definition of a message sent to an NT port on the CSRSS server. + * + * Not sure what dummy1/dummy2 do, but they're junk as far as I can see. + */ +struct csrss_message { + uint32_t dummy1; + uint32_t opcode; + uint32_t status; + uint32_t dummy2; +}; + +static inline void +inherit_handles(void) +{ + uint32_t n = 0x1000; + uint32_t *p = mowgli_alloc_array(sizeof(uint32_t), n); + uint32_t pid, i; + SYSTEM_HANDLE_INFORMATION *info; + + while (ZwQuerySystemInformation(SystemHandleInformation, p, n * sizeof(*p), 0) == STATUS_INFO_LENGTH_MISMATCH) + { + n *= 2; + + mowgli_free(p); + p = mowgli_alloc_array(sizeof(uint32_t), n); + } + + info = (SYSTEM_HANDLE_INFORMATION *) (p + 1); + pid = GetCurrentProcessId(); + + for (i = 0; i < *p; i++) + { + if (info[i].ProcessId == pid) + SetHandleInformation((HANDLE) h[i].Handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); + } + + mowgli_free(p); +} + +static inline void +request_csrss_session(HANDLE proc_handle, HANDLE thread_handle, uint32_t pid, uint32_t tid) +{ + struct { + PORT_MESSAGE port_message; + struct csrss_message csrss_message; + PROCESS_INFORMATION process_information; + CLIENT_ID debugger; + uint32_t flags; + uint32_t vdminfo[2]; + } csrmsg = {{0}, {0}, {proc_handle, thread_handle, pid, tid}, {0}, 0, {0}}; + + CsrCallClientServer(&csrmsg, NULL, 0x10000, sizeof csrmsg); +} + +int child(void) +{ + typedef BOOL (*CsrpConnectToServer)(PWSTR); + + CsrpConnectToServer (0x77F8F65D) (L"\\Windows"); + __asm__("mov eax, 0; mov esp, ebp; pop ebp; ret"); +} + +int fork(void) +{ + HANDLE proc_handle, thread_handle; + OBJECT_ATTRIBUTES oa = { sizeof(oa) }; + CONTEXT context = { CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS | CONTEXT_FLOATING_POINT }; + MEMORY_BASIC_INFORMATION mbi; + THREAD_BASIC_INFORMATION tbi; + PNT_TIB tib; + USER_STACK stack; + CLIENT_ID cid; + + /* ensure the child has the same handles and ports */ + inherit_handles(); + + /* create the actual LWP using ZwCreateProcess() */ + ZwCreateProcess(&proc_handle, PROCESS_ALL_ACCESS, &oa, NtCurrentProcess(), TRUE, 0, 0, 0); + + /* now set up a thread for that process using a context, cloning the current thread ... */ + ZwGetContextThread(NtCurrentThread(), &context); + context.Eip = (unsigned long) child; + + /* set up a stack for the thread now that the child sentinel is set up ... */ + ZwQueryVirtualMemory(NtCurrentProcess(), (void *) context.Esp, MemoryBasicInformation, + &mbi, sizeof mbi, 0); + + stack = (USER_STACK){0, 0, ((char *) mbi.BaseAddress) + mbi.RegionSize, + mbi.BaseAddress, mbi.AllocationBase}; + + /* now spawn the thread! */ + ZwCreateThread(&thread_handle, THREAD_ALL_ACCESS, &oa, proc_handle, &cid, &context, &stack, TRUE); + + /* thread is spawned, but frozen for inspection -- fix up memory protection before unfreezing */ + ZwQueryInformationThread(NtCurrentThread(), ThreadBasicInformation, &tbi, sizeof tbi, 0); + tib = tbi.TebBaseAddress; + + ZwQueryInformationThread(thread_handle, ThreadBasicInformation, &tbi, sizeof tbi, 0); + ZwWriteVirtualMemory(process_handle, tbi.TebBaseAddress, &tib->ExceptionList, sizeof(tib->ExceptionList), 0); + + /* ready to go, now request a CSRSS session */ + request_csrss_session(process_handle, thread_handle, (uint32_t) cid.UniqueProcess, (uint32_t) cid.UniqueThread); + + /* CSRSS session set up or we segfaulted by now, so unfreeze the child... */ + ZwResumeThread(thread_handle, 0); + + /* release handle refcount now that process is freestanding */ + ZwClose(thread_handle); + ZwClose(process_handle); + + return (int) cid.UniqueProcess; +} + +#endif + +#endif + +#endif diff --git a/src/libmowgli/platform/win32/gettimeofday.c b/src/libmowgli/platform/win32/gettimeofday.c new file mode 100644 index 0000000..1e96797 --- /dev/null +++ b/src/libmowgli/platform/win32/gettimeofday.c @@ -0,0 +1,66 @@ +/* + * libmowgli: A collection of useful routines for programming. + * win32_support.c: Support functions and values for Win32 platform. + * + * Copyright (c) 2009 SystemInPlace, Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mowgli.h" + +#ifdef _MSC_VER +# define EPOCH_TIME_IN_MICROSECS 11644473600000000Ui64 +#else +# define EPOCH_TIME_IN_MICROSECS 11644473600000000ULL +#endif + +#ifdef _WIN32 +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + ULARGE_INTEGER tmpres; + static mowgli_boolean_t tz_init_done = FALSE; + + if (tv != NULL) + { + GetSystemTimeAsFileTime(&ft); + + tmpres.u.HighPart = ft.dwHighDateTime; + tmpres.u.LowPart = ft.dwLowDateTime; + + tmpres.QuadPart /= 10; + tmpres.QuadPart -= EPOCH_TIME_IN_MICROSECS; + tv->tv_sec = (long) (tmpres.QuadPart / 1000000UL); + tv->tv_usec = (long) (tmpres.QuadPart % 1000000UL); + } + + if (tz != NULL) + { + if (!tz_init_done) + { + _tzset(); + tz_init_done = TRUE; + } + + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} +#endif diff --git a/src/libmowgli/platform/win32/inet.c b/src/libmowgli/platform/win32/inet.c new file mode 100644 index 0000000..051ae7f --- /dev/null +++ b/src/libmowgli/platform/win32/inet.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mowgli.h" + +#ifdef _WIN32 + +int inet_pton(int af, const char *src, void *dst) +{ + struct sockaddr_storage ss; + int size = sizeof(struct sockaddr_storage); + char src_copy[INET6_ADDRSTRLEN + 1]; + + mowgli_strlcpy(src_copy, src, sizeof src_copy); + + if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *) &ss, &size) != SOCKET_ERROR) + { + switch (af) + { + case AF_INET: + *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; + return 1; + + case AF_INET6: + *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; + return 1; + + default: + return 0; + } + } + + return -1; +} + +const char *inet_ntop(int af, const void *addr, char *host, size_t hostlen) +{ + struct sockaddr_storage ss; + int size = sizeof(struct sockaddr_storage); + + ss.ss_family = af; + + switch (af) + { + case AF_INET: + memcpy(&(((struct sockaddr_in *) &ss)->sin_addr), (struct in_addr *) addr, sizeof (struct in_addr)); + break; + + case AF_INET6: + memcpy(&(((struct sockaddr_in6 *) &ss)->sin6_addr), (struct in6_addr *) addr, sizeof (struct in6_addr)); + break; + + default: + return NULL; + } + + if (WSAAddressToString((struct sockaddr *) &ss, size, 0, host, (LPDWORD) &hostlen) != SOCKET_ERROR) + return host; + + return NULL; +} + +#endif diff --git a/src/libmowgli/platform/win32/pipe.c b/src/libmowgli/platform/win32/pipe.c new file mode 100644 index 0000000..5da3dd2 --- /dev/null +++ b/src/libmowgli/platform/win32/pipe.c @@ -0,0 +1,31 @@ +/* + * libmowgli: A collection of useful routines for programming. + * pipe.c: UNIX pipe emulation + * + * Copyright (c) 2012 TortoiseLabs, LLC. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mowgli.h" + +#ifdef _WIN32 +int pipe(int pipefd[2]) +{ + return socketpair(AF_INET, SOCK_STREAM, 0, pipefd); +} +#endif diff --git a/src/libmowgli/platform/win32/setenv.c b/src/libmowgli/platform/win32/setenv.c new file mode 100644 index 0000000..f80a574 --- /dev/null +++ b/src/libmowgli/platform/win32/setenv.c @@ -0,0 +1,31 @@ +/* + * libmowgli: A collection of useful routines for programming. + * setenv.c: setenv() wrapper around SetEnvironmentVariable(). + * + * Copyright (c) 2012 TortoiseLabs, LLC. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mowgli.h" + +#ifdef _WIN32 +int setenv(const char *name, const char *value, int overwrite) +{ + return !SetEnvironmentVariable(name, value); +} +#endif diff --git a/src/libmowgli/platform/win32/socketpair.c b/src/libmowgli/platform/win32/socketpair.c new file mode 100644 index 0000000..6ede51e --- /dev/null +++ b/src/libmowgli/platform/win32/socketpair.c @@ -0,0 +1,109 @@ +/* socketpair.c + * Copyright 2007, 2010 by Nathan C. Myers <ncm@cantrip.org> + * This code is Free Software. It may be copied freely, in original or + * modified form, subject only to the restrictions that (1) the author is + * relieved from all responsibilities for any use for any purpose, and (2) + * this copyright notice must be retained, unchanged, in its entirety. If + * for any reason the author might be held responsible for any consequences + * of copying or use, license is withheld. + */ + +/* Changes: + * 2010-03-31: + * set addr to 127.0.0.1 because win32 getsockname does not always set it. + * 2010-02-25: + * set SO_REUSEADDR option to avoid leaking some windows resource. + * Windows System Error 10049, "Event ID 4226 TCP/IP has reached + * the security limit imposed on the number of concurrent TCP connect + * attempts." Bleah. + * 2007-04-25: + * preserve value of WSAGetLastError() on all error returns. + * 2007-04-22: (Thanks to Matthew Gregan <kinetik@flim.org>) + * s/EINVAL/WSAEINVAL/ fix trivial compile failure + * s/socket/WSASocket/ enable creation of sockets suitable as stdin/stdout + * of a child process. + * add argument make_overlapped + */ + +#include "mowgli.h" + +#ifdef _WIN32 + +/* dumb_socketpair: + * If make_overlapped is nonzero, both sockets created will be usable for + * "overlapped" operations via WSASend etc. If make_overlapped is zero, + * socks[0] (only) will be usable with regular ReadFile etc., and thus + * suitable for use as stdin or stdout of a child process. Note that the + * sockets must be closed with closesocket() regardless. + */ + +int socketpair(int domain, int type, int protocol, int socks[2]) +{ + union { + struct sockaddr_in inaddr; + struct sockaddr addr; + } a; + SOCKET listener; + int e; + socklen_t addrlen = sizeof(a.inaddr); + int make_overlapped = 0; + DWORD flags = (make_overlapped ? WSA_FLAG_OVERLAPPED : 0); + int reuse = 1; + + if (socks == NULL) { + WSASetLastError(WSAEINVAL); + return SOCKET_ERROR; + } + + listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (listener == INVALID_SOCKET) + return SOCKET_ERROR; + + memset(&a, 0, sizeof(a)); + a.inaddr.sin_family = AF_INET; + a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + a.inaddr.sin_port = 0; + + socks[0] = socks[1] = INVALID_SOCKET; + do { + if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, + (char*) &reuse, (socklen_t) sizeof(reuse)) == -1) + break; + if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR) + break; + + memset(&a, 0, sizeof(a)); + if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR) + break; + // win32 getsockname may only set the port number, p=0.0005. + // ( http://msdn.microsoft.com/library/ms738543.aspx ): + a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + a.inaddr.sin_family = AF_INET; + + if (listen(listener, 1) == SOCKET_ERROR) + break; + + socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags); + if (socks[0] == (int) INVALID_SOCKET) + break; + if (connect(socks[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR) + break; + + socks[1] = accept(listener, NULL, NULL); + if (socks[1] == (int) INVALID_SOCKET) + break; + + closesocket(listener); + return 0; + + } while (0); + + e = WSAGetLastError(); + closesocket(listener); + closesocket(socks[0]); + closesocket(socks[1]); + WSASetLastError(e); + return SOCKET_ERROR; +} + +#endif diff --git a/src/libmowgli/platform/win32/win32_stdinc.h b/src/libmowgli/platform/win32/win32_stdinc.h new file mode 100644 index 0000000..1360a4a --- /dev/null +++ b/src/libmowgli/platform/win32/win32_stdinc.h @@ -0,0 +1,62 @@ +/* + * libmowgli: A collection of useful routines for programming. + * win32_stdinc.h: Support functions and values for Win32 platform. + * + * Copyright (c) 2009 SystemInPlace, Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __LIBMOWGLI_SRC_LIBMOWGLI_WIN32_SUPPORT_H__GUARD +#define __LIBMOWGLI_SRC_LIBMOWGLI_WIN32_SUPPORT_H__GUARD + +#ifdef _WIN32 + +#include <winsock2.h> +#include <ws2tcpip.h> +#include <time.h> + +#define strcasecmp _stricmp +#define strdup _strdup +#define usleep(_usecs) Sleep((_usecs)/1000L) +#ifdef _MSC_VER +# define snprintf _snprintf +#endif + +struct timezone { + int tz_minuteswest; + int tz_dsttime; +}; + +extern int gettimeofday(struct timeval *tv, struct timezone *tz); +extern int setenv(const char *name, const char *value, int overwrite); +extern int pipe(int pipefd[2]); +extern int socketpair(int domain, int type, int protocol, int pipefd[2]); +extern int fork(void); +extern int inet_pton(int af, const char *src, void *dst); +extern const char *inet_ntop(int af, const void *addr, char *host, size_t hostlen); + +/* MSYS autoconf is fucko. */ +#ifndef HAVE_WINSOCK2_H +#define HAVE_WINSOCK2_H +#endif + +#define HAVE_SELECT + +#endif + +#endif |