From f1b7ead828e5afcc88f9b3019d87d16ff8c7fd42 Mon Sep 17 00:00:00 2001 From: basil00 Date: Sat, 15 Oct 2011 17:40:18 +0800 Subject: [PATCH] The divert.dll library can now be build with MinGW Linux cross-compilation. This is useful for projects that use MinGW instead of MSVC. Otherwise, the .dlls are incompatible. --- build.sh | 38 ++++++++++++++++++++++++++++++++++++++ dll/divert.c | 42 ++++++++++++++++++++++++++++++++++++++++++ doc/divert.html | 7 +++++++ include/divert.h | 7 +++++++ 4 files changed, 94 insertions(+) create mode 100644 build.sh diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..7f47c57 --- /dev/null +++ b/build.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# +# build.sh +# (C) 2011, all rights reserved, +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Script for MinGW/Linux cross compilation. +# NOTE: This script only builds divert.dll, not the driver divert.sys + +ENVS="i586-mingw32msvc amd64-mingw32msvc" + +for ENV in $ENVS +do + CC="$ENV-gcc" + if [ -x "`which $CC`" ] + then + mkdir -p "install/$ENV" + echo "$CC -Iinclude/ -c divert.o dll/divert.c" + $CC -Iinclude/ -c divert.o dll/divert.c + echo "$CC -shared -o install/$ENV/divert.dll divert.o" + $CC -shared -o "install/$ENV/divert.dll" divert.o + else + echo "$CC: not found" + fi +done + diff --git a/dll/divert.c b/dll/divert.c index 2012b1b..d3add68 100644 --- a/dll/divert.c +++ b/dll/divert.c @@ -26,7 +26,36 @@ #include #include #include + +#ifdef __MINGW32__ +// Mingw does not have wdfinstaller.h +typedef struct _WDF_COINSTALLER_INSTALL_OPTIONS +{ + ULONG Size; + BOOL ShowRebootPrompt; +} WDF_COINSTALLER_INSTALL_OPTIONS, *PWDF_COINSTALLER_INSTALL_OPTIONS; + +VOID FORCEINLINE WDF_COINSTALLER_INSTALL_OPTIONS_INIT( + PWDF_COINSTALLER_INSTALL_OPTIONS ClientOptions) +{ + RtlZeroMemory(ClientOptions, sizeof(WDF_COINSTALLER_INSTALL_OPTIONS)); + ClientOptions->Size = sizeof(WDF_COINSTALLER_INSTALL_OPTIONS); +} +typedef ULONG (WINAPI *PFN_WDFPREDEVICEINSTALLEX)( + LPCWSTR inf_path, + LPCWSTR inf_sec_name, + PWDF_COINSTALLER_INSTALL_OPTIONS options + ); +typedef ULONG (WINAPI *PFN_WDFPOSTDEVICEINSTALL)( + LPCWSTR inf_path, + LPCWSTR inf_sec_name + ); +// Mingw does not support UINT8 and UINT16 ??? +#define UINT8 unsigned char +#define UINT16 unsigned short +#else /* __MINGW32__ */ #include +#endif /* __MINGW32__ */ // #define DIVERT_DEBUG @@ -39,6 +68,19 @@ #define DIVERT_DRIVER_INF L"\\" DIVERT_DRIVER_NAME L".inf" #define DIVERT_DRIVER_MATCH_DLL L"\\WdfCoInstaller*.dll" +/* + * ntoh and hton implementation to remove winsock dependency. + */ +#define BYTESWAP16(x) \ + ((((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00)) +#define BYTESWAP32(x) \ + ((((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) | \ + (((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000)) +#define ntohs(x) BYTESWAP16(x) +#define htons(x) BYTESWAP16(x) +#define ntohl(x) BYTESWAP32(x) +#define htonl(x) BYTESWAP32(x) + /* * Filter parsing. */ diff --git a/doc/divert.html b/doc/divert.html index e922069..445e79f 100644 --- a/doc/divert.html +++ b/doc/divert.html @@ -109,6 +109,13 @@ This will build the following files and place them in the

+

+MinGW version of divert.dll may be built in Linux using the +build.sh script in the root directory. +This is useful for projects that use MinGW instead of MSVC. +Otherwise, the .dlls from different compilers are generally +incompatible. +

2.2 Driver Signing

diff --git a/include/divert.h b/include/divert.h index ebda134..8b1fa47 100644 --- a/include/divert.h +++ b/include/divert.h @@ -25,6 +25,13 @@ #define DIVERTEXPORT __declspec(dllimport) #endif /* DIVERTEXPORT */ +#ifdef __MINGW32__ +#define __in +#define __out +#define __out_opt +#define __inout +#endif /* __MINGW32__ */ + #ifdef __cplusplus extern "C" { #endif