Remove the WinDivert.dll dependency on MSVCRT*.dll
The MSVCRT*.dll dependency has been removed by: (1) using Win32 API equivalents where possible, and (2) re-implementing some basic C runtime functions. This prevents multiple MSVCRTs from being loaded at runtime (if the application uses a different version than the DLL). The new WinDivert.dll only depends on KERNEL32.dll and ADVAPI32.dll. Furthermore, the MINGW build has been extended to generate a WinDivert.lib import library file. This means that the MINGW DLL can now be used directly by Visual Studio (any version).
This commit is contained in:
@@ -125,3 +125,5 @@ WinDivert 1.4.1
|
||||
- Dual license WinDivert under LGPLv3 and GPLv2.
|
||||
WinDivert 1.4.2
|
||||
- Add workaround for pseudo checksum issue (see #134).
|
||||
WinDivert 1.4.3
|
||||
- WinDivert.dll no longer depends on MSVCRT*.dll.
|
||||
|
||||
+53
-3
@@ -39,7 +39,7 @@
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <winioctl.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -64,10 +64,15 @@
|
||||
#define ntohl(x) BYTESWAP32(x)
|
||||
#define htonl(x) BYTESWAP32(x)
|
||||
|
||||
static BOOLEAN WinDivertIsXDigit(char c);
|
||||
static BOOLEAN WinDivertIsSpace(char c);
|
||||
static BOOLEAN WinDivertIsAlNum(char c);
|
||||
static char WinDivertToLower(char c);
|
||||
static BOOLEAN WinDivertStrLen(const wchar_t *s, size_t maxlen,
|
||||
size_t *lenptr);
|
||||
static BOOLEAN WinDivertStrCpy(wchar_t *dst, size_t dstlen,
|
||||
const wchar_t *src);
|
||||
static int WinDivertStrCmp(const char *s, const char *t);
|
||||
static BOOLEAN WinDivertAToI(const char *str, char **endptr, UINT32 *intptr);
|
||||
static BOOLEAN WinDivertAToX(const char *str, char **endptr, UINT32 *intptr);
|
||||
|
||||
@@ -694,6 +699,33 @@ extern BOOL WinDivertGetParam(HANDLE handle, WINDIVERT_PARAM param,
|
||||
/* REPLACEMENTS */
|
||||
/*****************************************************************************/
|
||||
|
||||
static BOOLEAN WinDivertIsXDigit(char c)
|
||||
{
|
||||
return (c >= '0' && c <= '9') ||
|
||||
(c >= 'a' && c <= 'f') ||
|
||||
(c >= 'A' && c <= 'F');
|
||||
}
|
||||
|
||||
static BOOLEAN WinDivertIsSpace(char c)
|
||||
{
|
||||
return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
|
||||
c == '\v');
|
||||
}
|
||||
|
||||
static BOOLEAN WinDivertIsAlNum(char c)
|
||||
{
|
||||
return (c >= 'a' && c <= 'z') ||
|
||||
(c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9');
|
||||
}
|
||||
|
||||
static char WinDivertToLower(char c)
|
||||
{
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
return 'a' + (c - 'A');
|
||||
return c;
|
||||
}
|
||||
|
||||
static BOOLEAN WinDivertStrLen(const wchar_t *s, size_t maxlen,
|
||||
size_t *lenptr)
|
||||
{
|
||||
@@ -728,6 +760,24 @@ static BOOLEAN WinDivertStrCpy(wchar_t *dst, size_t dstlen, const wchar_t *src)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static int WinDivertStrCmp(const char *s, const char *t)
|
||||
{
|
||||
int cmp;
|
||||
size_t i;
|
||||
for (i = 0; ; i++)
|
||||
{
|
||||
cmp = s[i] - t[i];
|
||||
if (cmp != 0)
|
||||
{
|
||||
return cmp;
|
||||
}
|
||||
if (s[i] == '\0')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static BOOLEAN WinDivertAToI(const char *str, char **endptr, UINT32 *intptr)
|
||||
{
|
||||
size_t i = 0;
|
||||
@@ -766,7 +816,7 @@ static BOOLEAN WinDivertAToX(const char *str, char **endptr, UINT32 *intptr)
|
||||
{
|
||||
i += 2;
|
||||
}
|
||||
for (; str[i] && isxdigit(str[i]); i++)
|
||||
for (; str[i] && WinDivertIsXDigit(str[i]); i++)
|
||||
{
|
||||
num0 = num;
|
||||
num *= 16;
|
||||
@@ -776,7 +826,7 @@ static BOOLEAN WinDivertAToX(const char *str, char **endptr, UINT32 *intptr)
|
||||
}
|
||||
else
|
||||
{
|
||||
num += (UINT32)(tolower(str[i]) - 'a') + 0x0A;
|
||||
num += (UINT32)(WinDivertToLower(str[i]) - 'a') + 0x0A;
|
||||
}
|
||||
if (num0 > num)
|
||||
{
|
||||
|
||||
+10
-10
@@ -709,7 +709,7 @@ extern BOOL WinDivertHelperParseIPv6Address(const char *str, UINT32 *addr_ptr)
|
||||
left = FALSE;
|
||||
str++;
|
||||
}
|
||||
for (l = 0; l < 4 && isxdigit(*str); l++)
|
||||
for (l = 0; l < 4 && WinDivertIsXDigit(*str); l++)
|
||||
{
|
||||
part_str[l] = *str;
|
||||
str++;
|
||||
@@ -786,7 +786,7 @@ static PTOKEN_NAME WinDivertTokenLookup(PTOKEN_NAME token_names,
|
||||
while (hi >= lo)
|
||||
{
|
||||
mid = (lo + hi) / 2;
|
||||
cmp = strcmp(token_names[mid].name, name);
|
||||
cmp = WinDivertStrCmp(token_names[mid].name, name);
|
||||
if (cmp < 0)
|
||||
{
|
||||
lo = mid+1;
|
||||
@@ -889,7 +889,7 @@ static ERROR WinDivertTokenizeFilter(const char *filter, WINDIVERT_LAYER layer,
|
||||
return MAKE_ERROR(WINDIVERT_ERROR_TOO_LONG, i);
|
||||
}
|
||||
memset(tokens[tp].val, 0, sizeof(tokens[tp].val));
|
||||
while (isspace(filter[i]))
|
||||
while (WinDivertIsSpace(filter[i]))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
@@ -974,11 +974,11 @@ static ERROR WinDivertTokenizeFilter(const char *filter, WINDIVERT_LAYER layer,
|
||||
break;
|
||||
}
|
||||
token[0] = c;
|
||||
if (isalnum(c) || c == '.' || c == ':')
|
||||
if (WinDivertIsAlNum(c) || c == '.' || c == ':')
|
||||
{
|
||||
UINT32 num;
|
||||
char *end;
|
||||
for (j = 1; j < TOKEN_MAXLEN && (isalnum(filter[i]) ||
|
||||
for (j = 1; j < TOKEN_MAXLEN && (WinDivertIsAlNum(filter[i]) ||
|
||||
filter[i] == '.' || filter[i] == ':'); j++, i++)
|
||||
{
|
||||
token[j] = filter[i];
|
||||
@@ -1995,7 +1995,7 @@ static ERROR WinDivertCompileFilter(const char *filter,
|
||||
}
|
||||
|
||||
// Allocate memory pool for the compiler:
|
||||
pool = (PPOOL)malloc(sizeof(POOL));
|
||||
pool = (PPOOL)HeapAlloc(GetProcessHeap(), 0, sizeof(POOL));
|
||||
if (pool == NULL)
|
||||
{
|
||||
return MAKE_ERROR(WINDIVERT_ERROR_NO_MEMORY, 0);
|
||||
@@ -2010,12 +2010,12 @@ static ERROR WinDivertCompileFilter(const char *filter,
|
||||
if (expr == NULL)
|
||||
{
|
||||
error = pool->error;
|
||||
free(pool);
|
||||
HeapFree(GetProcessHeap(), 0, pool);
|
||||
return error;
|
||||
}
|
||||
if (tokens[i].kind != TOKEN_END)
|
||||
{
|
||||
free(pool);
|
||||
HeapFree(GetProcessHeap(), 0, pool);
|
||||
return MAKE_ERROR(WINDIVERT_ERROR_UNEXPECTED_TOKEN, tokens[i].pos);
|
||||
}
|
||||
|
||||
@@ -2025,7 +2025,7 @@ static ERROR WinDivertCompileFilter(const char *filter,
|
||||
WINDIVERT_FILTER_RESULT_REJECT, stack);
|
||||
if (label < 0)
|
||||
{
|
||||
free(pool);
|
||||
HeapFree(GetProcessHeap(), 0, pool);
|
||||
return MAKE_ERROR(WINDIVERT_ERROR_TOO_LONG, 0);
|
||||
}
|
||||
|
||||
@@ -2034,7 +2034,7 @@ static ERROR WinDivertCompileFilter(const char *filter,
|
||||
{
|
||||
WinDivertEmitFilter(stack, label, label, object, obj_len);
|
||||
}
|
||||
free(pool);
|
||||
HeapFree(GetProcessHeap(), 0, pool);
|
||||
|
||||
return MAKE_ERROR(WINDIVERT_ERROR_NONE, 0);
|
||||
}
|
||||
|
||||
+8
-3
@@ -54,15 +54,16 @@ do
|
||||
if [ ! -d install/WDDK/$CPU ]
|
||||
then
|
||||
echo "WARNING: missing WDDK build; run wddk-build.bat first"
|
||||
echo "SKIP WDDK-$CPU"
|
||||
echo "SKIP MINGW-$CPU"
|
||||
continue
|
||||
fi
|
||||
echo "BUILD WDDK-$CPU"
|
||||
echo "BUILD MINGW-$CPU"
|
||||
CC="$ENV-gcc"
|
||||
COPTS="-shared -Wall -Wno-pointer-to-int-cast -O2 -Iinclude/
|
||||
-Wl,--enable-stdcall-fixup -Wl,--entry=${MANGLE}WinDivertDllEntry"
|
||||
CLIBS="-lgcc -lmsvcrt -lkernel32 -ladvapi32"
|
||||
CLIBS="-lgcc -lkernel32 -ladvapi32"
|
||||
STRIP="$ENV-strip"
|
||||
DLLTOOL="$ENV-dlltool"
|
||||
if [ -x "`which $CC`" ]
|
||||
then
|
||||
echo "\tmake install/MINGW/$CPU..."
|
||||
@@ -72,6 +73,10 @@ do
|
||||
$CC $COPTS -o "install/MINGW/$CPU/WinDivert.dll" \
|
||||
dll/windivert.o dll/windivert.def -nostdlib $CLIBS
|
||||
$STRIP "install/MINGW/$CPU/WinDivert.dll"
|
||||
echo "\tbuild install/MINGW/$CPU/WinDivert.lib..."
|
||||
$DLLTOOL --dllname install/MINGW/$CPU/WinDivert.dll \
|
||||
--def dll/windivert.def \
|
||||
--output-lib install/MINGW/$CPU/WinDivert.lib 2>/dev/null
|
||||
echo "\tbuild install/MINGW/$CPU/netdump.exe..."
|
||||
$CC -s -O2 -Iinclude/ examples/netdump/netdump.c \
|
||||
-o "install/MINGW/$CPU/netdump.exe" -lWinDivert -lws2_32 \
|
||||
|
||||
+4
-10
@@ -76,11 +76,8 @@ do
|
||||
then
|
||||
echo "\t\033[33mWARNING\033[0m: unsigned WinDivert32.sys..."
|
||||
fi
|
||||
if [ $TARGET != MINGW ]
|
||||
then
|
||||
echo "\tcopy $INSTALL/x86/WinDivert.lib..."
|
||||
cp install/$TARGET/i386/WinDivert.lib $INSTALL/x86
|
||||
fi
|
||||
echo "\tcopy $INSTALL/x86/WinDivert.lib..."
|
||||
cp install/$TARGET/i386/WinDivert.lib $INSTALL/x86
|
||||
echo "\tcopy $INSTALL/x86/WinDivert.dll..."
|
||||
cp install/$TARGET/i386/WinDivert.dll $INSTALL/x86
|
||||
echo "\tcopy $INSTALL/x86/netdump.exe..."
|
||||
@@ -104,11 +101,8 @@ do
|
||||
then
|
||||
echo -e "\t\033[33mWARNING\033[0m: unsigned WinDivert64.sys..."
|
||||
fi
|
||||
if [ $TARGET != MINGW ]
|
||||
then
|
||||
echo "\tcopy $INSTALL/amd64/WinDivert.lib..."
|
||||
cp install/$TARGET/amd64/WinDivert.lib $INSTALL/amd64
|
||||
fi
|
||||
echo "\tcopy $INSTALL/amd64/WinDivert.lib..."
|
||||
cp install/$TARGET/amd64/WinDivert.lib $INSTALL/amd64
|
||||
echo "\tcopy $INSTALL/amd64/WinDivert.dll..."
|
||||
cp install/$TARGET/amd64/WinDivert.dll $INSTALL/amd64
|
||||
echo "\tcopy $INSTALL/amd64/netdump.exe..."
|
||||
|
||||
Reference in New Issue
Block a user