Skip to content

Commit 5d62e2a

Browse files
committed
Add a Visual Studio build system (fix basil00#118).
Currently the VS build system targets VS2015, but may also work for later versions (not tested). To use: - Download & install VS2015. - Install WDK. - Open a Developer Command Prompt. - Run the msvc-build.bat script.
1 parent 8c4bbf3 commit 5d62e2a

25 files changed

+1540
-288
lines changed

dll/windivert.c

+118-11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define ERROR_DRIVER_FAILED_PRIOR_UNLOAD ((DWORD)654)
5757
#endif
5858

59+
static BOOLEAN WinDivertIsDigit(char c);
5960
static BOOLEAN WinDivertIsXDigit(char c);
6061
static BOOLEAN WinDivertIsSpace(char c);
6162
static BOOLEAN WinDivertIsAlNum(char c);
@@ -69,17 +70,45 @@ static BOOLEAN WinDivertAToI(const char *str, char **endptr, UINT32 *intptr,
6970
UINT size);
7071
static BOOLEAN WinDivertAToX(const char *str, char **endptr, UINT32 *intptr,
7172
UINT size, BOOL prefix);
73+
static UINT32 WinDivertDivTen128(UINT32 *a);
7274

7375
/*
7476
* Misc.
7577
*/
7678
#ifndef UINT8_MAX
7779
#define UINT8_MAX 0xFF
7880
#endif
81+
#ifndef UINT16_MAX
82+
#define UINT16_MAX 0xFFFF
83+
#endif
7984
#ifndef UINT32_MAX
8085
#define UINT32_MAX 0xFFFFFFFF
8186
#endif
8287

88+
#ifdef _MSC_VER
89+
90+
#pragma intrinsic(memcpy)
91+
#pragma function(memcpy)
92+
void *memcpy(void *dst, const void *src, size_t n)
93+
{
94+
size_t i;
95+
for (i = 0; i < n; i++)
96+
((UINT8 *)dst)[i] = ((const UINT8 *)src)[i];
97+
return dst;
98+
}
99+
100+
#pragma intrinsic(memset)
101+
#pragma function(memset)
102+
void *memset(void *dst, int c, size_t n)
103+
{
104+
size_t i;
105+
for (i = 0; i < n; i++)
106+
((UINT8 *)dst)[i] = (UINT8)c;
107+
return dst;
108+
}
109+
110+
#endif
111+
83112
/*
84113
* Prototypes.
85114
*/
@@ -377,11 +406,11 @@ static BOOL WinDivertIoControl(HANDLE handle, DWORD code,
377406
extern HANDLE WinDivertOpen(const char *filter, WINDIVERT_LAYER layer,
378407
INT16 priority, UINT64 flags)
379408
{
380-
WINDIVERT_FILTER object[WINDIVERT_FILTER_MAXLEN];
409+
WINDIVERT_FILTER *object;
381410
UINT obj_len;
382411
ERROR comp_err;
383412
DWORD err;
384-
HANDLE handle;
413+
HANDLE handle, pool;
385414
UINT64 filter_flags;
386415
WINDIVERT_IOCTL ioctl;
387416
WINDIVERT_VERSION version;
@@ -393,7 +422,7 @@ extern HANDLE WinDivertOpen(const char *filter, WINDIVERT_LAYER layer,
393422
offsetof(WINDIVERT_DATA_SOCKET, Protocol) != 56 ||
394423
offsetof(WINDIVERT_DATA_REFLECT, Priority) != 24 ||
395424
sizeof(WINDIVERT_FILTER) != 24 ||
396-
offsetof(WINDIVERT_ADDRESS, Reserved2) != 16)
425+
offsetof(WINDIVERT_ADDRESS, Reserved3) != 16)
397426
{
398427
SetLastError(ERROR_INVALID_PARAMETER);
399428
return INVALID_HANDLE_VALUE;
@@ -426,9 +455,25 @@ extern HANDLE WinDivertOpen(const char *filter, WINDIVERT_LAYER layer,
426455
}
427456

428457
// Compile & analyze the filter:
429-
comp_err = WinDivertCompileFilter(filter, layer, object, &obj_len);
458+
pool = HeapCreate(HEAP_NO_SERIALIZE, WINDIVERT_MIN_POOL_SIZE,
459+
WINDIVERT_MAX_POOL_SIZE);
460+
if (pool == NULL)
461+
{
462+
return FALSE;
463+
}
464+
object = HeapAlloc(pool, 0,
465+
WINDIVERT_FILTER_MAXLEN * sizeof(WINDIVERT_FILTER));
466+
if (object == NULL)
467+
{
468+
err = GetLastError();
469+
HeapDestroy(pool);
470+
SetLastError(err);
471+
return FALSE;
472+
}
473+
comp_err = WinDivertCompileFilter(filter, pool, layer, object, &obj_len);
430474
if (IS_ERROR(comp_err))
431475
{
476+
HeapDestroy(pool);
432477
SetLastError(ERROR_INVALID_PARAMETER);
433478
return INVALID_HANDLE_VALUE;
434479
}
@@ -443,31 +488,36 @@ extern HANDLE WinDivertOpen(const char *filter, WINDIVERT_LAYER layer,
443488
err = GetLastError();
444489
if (err != ERROR_FILE_NOT_FOUND && err != ERROR_PATH_NOT_FOUND)
445490
{
491+
HeapDestroy(pool);
492+
SetLastError(err);
446493
return INVALID_HANDLE_VALUE;
447494
}
448495

449496
// Open failed because the device isn't installed; install it now.
450497
if ((flags & WINDIVERT_FLAG_NO_INSTALL) != 0)
451498
{
499+
HeapDestroy(pool);
452500
SetLastError(ERROR_SERVICE_DOES_NOT_EXIST);
453501
return INVALID_HANDLE_VALUE;
454502
}
455503
SetLastError(0);
456504
if (!WinDivertDriverInstall())
457505
{
458-
if (GetLastError() == 0)
459-
{
460-
SetLastError(ERROR_OPEN_FAILED);
461-
}
506+
err = GetLastError();
507+
err = (err == 0? ERROR_OPEN_FAILED: err);
508+
HeapDestroy(pool);
509+
SetLastError(err);
462510
return INVALID_HANDLE_VALUE;
463511
}
464512
handle = CreateFile(L"\\\\.\\" WINDIVERT_DEVICE_NAME,
465513
GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
466514
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
467515
INVALID_HANDLE_VALUE);
468-
469516
if (handle == INVALID_HANDLE_VALUE)
470517
{
518+
err = GetLastError();
519+
HeapDestroy(pool);
520+
SetLastError(err);
471521
return INVALID_HANDLE_VALUE;
472522
}
473523
}
@@ -485,13 +535,17 @@ extern HANDLE WinDivertOpen(const char *filter, WINDIVERT_LAYER layer,
485535
if (!WinDivertIoControl(handle, IOCTL_WINDIVERT_INITIALIZE, &ioctl,
486536
&version, sizeof(version), NULL))
487537
{
538+
err = GetLastError();
488539
CloseHandle(handle);
540+
HeapDestroy(pool);
541+
SetLastError(err);
489542
return INVALID_HANDLE_VALUE;
490543
}
491544
if (version.magic != WINDIVERT_MAGIC_SYS ||
492545
version.major < WINDIVERT_VERSION_MAJOR_MIN)
493546
{
494547
CloseHandle(handle);
548+
HeapDestroy(pool);
495549
SetLastError(ERROR_DRIVER_FAILED_PRIOR_UNLOAD);
496550
return INVALID_HANDLE_VALUE;
497551
}
@@ -502,9 +556,13 @@ extern HANDLE WinDivertOpen(const char *filter, WINDIVERT_LAYER layer,
502556
if (!WinDivertIoControl(handle, IOCTL_WINDIVERT_STARTUP, &ioctl,
503557
object, obj_len * sizeof(WINDIVERT_FILTER), NULL))
504558
{
559+
err = GetLastError();
505560
CloseHandle(handle);
561+
HeapDestroy(pool);
562+
SetLastError(err);
506563
return INVALID_HANDLE_VALUE;
507564
}
565+
HeapDestroy(pool);
508566

509567
// Success!
510568
return handle;
@@ -645,6 +703,11 @@ extern BOOL WinDivertGetParam(HANDLE handle, WINDIVERT_PARAM param,
645703
/* REPLACEMENTS */
646704
/*****************************************************************************/
647705

706+
static BOOLEAN WinDivertIsDigit(char c)
707+
{
708+
return (c >= '0' && c <= '9');
709+
}
710+
648711
static BOOLEAN WinDivertIsXDigit(char c)
649712
{
650713
return (c >= '0' && c <= '9') ||
@@ -756,7 +819,7 @@ static BOOLEAN WinDivertAToI(const char *str, char **endptr, UINT32 *intptr,
756819
size_t i = 0;
757820
UINT32 n[4] = {0};
758821
BOOLEAN result = TRUE;
759-
for (; str[i] && isdigit(str[i]); i++)
822+
for (; str[i] && WinDivertIsDigit(str[i]); i++)
760823
{
761824
if (!WinDivertMul128(n, 10) || !WinDivertAdd128(n, str[i] - '0'))
762825
{
@@ -801,7 +864,7 @@ static BOOLEAN WinDivertAToX(const char *str, char **endptr, UINT32 *intptr,
801864
}
802865
for (; str[i] && WinDivertIsXDigit(str[i]); i++)
803866
{
804-
if (isdigit(str[i]))
867+
if (WinDivertIsDigit(str[i]))
805868
{
806869
dig = (UINT32)(str[i] - '0');
807870
}
@@ -833,3 +896,47 @@ static BOOLEAN WinDivertAToX(const char *str, char **endptr, UINT32 *intptr,
833896
return result;
834897
}
835898

899+
/*
900+
* Divide by 10 and return the remainder.
901+
*/
902+
#define WINDIVERT_BIG_MUL_ROUND(a, c, r, i) \
903+
do { \
904+
UINT64 t = WINDIVERT_MUL64((UINT64)(a), (UINT64)(c)); \
905+
UINT k; \
906+
for (k = (i); k < 9 && t != 0; k++) \
907+
{ \
908+
UINT64 s = (UINT64)(r)[k] + (t & 0xFFFFFFFF); \
909+
(r)[k] = (UINT32)s; \
910+
t = (t >> 32) + (s >> 32); \
911+
} \
912+
} while (FALSE)
913+
static UINT32 WinDivertDivTen128(UINT32 *a)
914+
{
915+
const UINT32 c[5] =
916+
{
917+
0x9999999A, 0x99999999, 0x99999999, 0x99999999, 0x19999999
918+
};
919+
UINT32 r[9] = {0}, m[6] = {0};
920+
UINT i, j;
921+
922+
for (i = 0; i < 4; i++)
923+
{
924+
for (j = 0; j < 5; j++)
925+
{
926+
WINDIVERT_BIG_MUL_ROUND(a[i], c[j], r, i+j);
927+
}
928+
}
929+
930+
a[0] = r[5];
931+
a[1] = r[6];
932+
a[2] = r[7];
933+
a[3] = r[8];
934+
935+
for (i = 0; i < 5; i++)
936+
{
937+
WINDIVERT_BIG_MUL_ROUND(r[i], 10, m, i);
938+
}
939+
940+
return m[5];
941+
}
942+

dll/windivert.def

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
LIBRARY WinDivert
22
EXPORTS
3-
WinDivertDllEntry
43
WinDivertOpen
54
WinDivertRecv
65
WinDivertRecvEx

dll/windivert.vcxproj

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
windivert.vcxproj
5+
(C) 2019, all rights reserved,
6+
7+
This file is part of WinDivert.
8+
9+
WinDivert is free software: you can redistribute it and/or modify it under
10+
the terms of the GNU Lesser General Public License as published by the
11+
Free Software Foundation, either version 3 of the License, or (at your
12+
option) any later version.
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17+
License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public License
20+
along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
WinDivert is free software; you can redistribute it and/or modify it under
23+
the terms of the GNU General Public License as published by the Free
24+
Software Foundation; either version 2 of the License, or (at your option)
25+
any later version.
26+
27+
This program is distributed in the hope that it will be useful, but
28+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
29+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30+
for more details.
31+
32+
You should have received a copy of the GNU General Public License along
33+
with this program; if not, write to the Free Software Foundation, Inc., 51
34+
Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35+
36+
-->
37+
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
38+
<ItemGroup Label="ProjectConfigurations">
39+
<ProjectConfiguration Include="Release|Win32">
40+
<Configuration>Release</Configuration>
41+
<Platform>Win32</Platform>
42+
</ProjectConfiguration>
43+
<ProjectConfiguration Include="Release|x64">
44+
<Configuration>Release</Configuration>
45+
<Platform>x64</Platform>
46+
</ProjectConfiguration>
47+
</ItemGroup>
48+
<ItemGroup>
49+
<ClCompile Include="windivert.c">
50+
<TreatWarningAsError>false</TreatWarningAsError>
51+
<Optimization>MinSpace</Optimization>
52+
<IntrinsicFunctions>true</IntrinsicFunctions>
53+
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
54+
<BufferSecurityCheck>false</BufferSecurityCheck>
55+
<AdditionalIncludeDirectories>..\include</AdditionalIncludeDirectories>
56+
</ClCompile>
57+
</ItemGroup>
58+
<PropertyGroup Label="Globals">
59+
<RootNamespace>WinDivert</RootNamespace>
60+
<ProjectName>WinDivert</ProjectName>
61+
</PropertyGroup>
62+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props"/>
63+
<PropertyGroup Label="Configuration">
64+
<PlatformToolset>v140</PlatformToolset>
65+
<UseDebugLibraries>true</UseDebugLibraries>
66+
<ConfigurationType>DynamicLibrary</ConfigurationType>
67+
</PropertyGroup>
68+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
69+
<ItemDefinitionGroup>
70+
<ClCompile>
71+
<WppEnabled>false</WppEnabled>
72+
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">WIN32;NDEBUG;_WINDOWS;_USRDLL;DLL_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
73+
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">WIN32;NDEBUG;_WINDOWS;_USRDLL;DLL_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
74+
</ClCompile>
75+
<Link>
76+
<EntryPointSymbol>WinDivertDllEntry</EntryPointSymbol>
77+
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
78+
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
79+
<ModuleDefinitionFile>windivert.def</ModuleDefinitionFile>
80+
<ImportLibrary>WinDivert.lib</ImportLibrary>
81+
</Link>
82+
</ItemDefinitionGroup>
83+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
84+
</Project>

0 commit comments

Comments
 (0)