From 5d5b46e5140d046e68e95be02ecb421d330b8b32 Mon Sep 17 00:00:00 2001 From: mark andrews Date: Sun, 11 Sep 2022 11:51:06 -0700 Subject: Add proj and sln for compile of dynamic x64 lib under Visual Studio 2019. * add CI support via MSBuild Signed-off-by: Toni Uhlig --- .github/workflows/build-msbuild.yml | 34 +++ .github/workflows/build.yml | 8 +- .gitignore | 97 ++++--- src/lib/ndpi_utils.c | 2 +- src/lib/third_party/src/gcrypt/digest.c | 2 +- src/lib/third_party/src/roaring.cc | 30 +-- windows/nDPI.sln | 25 ++ windows/nDPI.vcxproj | 454 ++++++++++++++++++++++++++++++++ windows/nDPI.vcxproj.filters | 298 +++++++++++++++++++++ windows/src/getopt.c | 117 ++++++++ windows/src/getopt.h | 6 + windows/src/win-gettimeofday.c | 55 ++++ 12 files changed, 1064 insertions(+), 64 deletions(-) create mode 100644 .github/workflows/build-msbuild.yml create mode 100644 windows/nDPI.sln create mode 100644 windows/nDPI.vcxproj create mode 100644 windows/nDPI.vcxproj.filters create mode 100644 windows/src/getopt.c create mode 100644 windows/src/getopt.h create mode 100644 windows/src/win-gettimeofday.c diff --git a/.github/workflows/build-msbuild.yml b/.github/workflows/build-msbuild.yml new file mode 100644 index 000000000..289c665ac --- /dev/null +++ b/.github/workflows/build-msbuild.yml @@ -0,0 +1,34 @@ +name: MSBuild +on: + push: + branches: + - dev + pull_request: + branches: + - dev + types: [opened, synchronize, reopened] + release: + types: [created] +jobs: + test: + name: MSBuild + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Add msbuild to PATH + uses: microsoft/setup-msbuild@v1.1 + + - name: Build solution for release + run: msbuild windows/nDPI.sln -t:rebuild -property:Configuration=Release + + - name: Build solution for debug + run: msbuild windows/nDPI.sln -t:rebuild -property:Configuration=Debug + + - name: Upload artifacts + uses: actions/upload-artifact@v2 + with: + name: build-artifacts + path: x64 + retention-days: 14 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6688322bb..c982c310f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -342,14 +342,14 @@ jobs: if: startsWith(matrix.os, 'ubuntu') && startsWith(matrix.arch, 'x86_64') && startsWith(matrix.compiler, 'clang') run: | sudo apt-get install ${{ matrix.compiler }} - - name: Install Windows prerequisites + - name: Install Windows msys2 prerequisites if: startsWith(matrix.os, 'windows') uses: msys2/setup-msys2@v2 with: msystem: MINGW64 update: true install: git mingw-w64-x86_64-toolchain automake1.16 automake-wrapper autoconf libtool make mingw-w64-x86_64-json-c mingw-w64-x86_64-crt-git mingw-w64-x86_64-pcre mingw-w64-x86_64-libpcap - - name: Setup Windows prerequisites (libgcrypt) + - name: Setup Windows msys2 prerequisites (libgcrypt) if: startsWith(matrix.os, 'windows') && startsWith(matrix.gcrypt, '--with-local-libgcrypt') run: | msys2 -c 'git clone --branch libgpg-error-1.42 https://github.com/gpg/libgpg-error' @@ -395,11 +395,11 @@ jobs: if: startsWith(matrix.os, 'macOS') && startsWith(matrix.arch, 'x86_64') && startsWith(matrix.compiler, 'cc') run: | ./autogen.sh --enable-option-checking=fatal --enable-debug-messages ${{ matrix.gcrypt }} ${{ matrix.msan }} ${{ matrix.pcre }} ${{ matrix.maxminddb }} --enable-tls-sigs - - name: Configure nDPI on Windows + - name: Configure nDPI on Windows msys2 if: startsWith(matrix.os, 'windows') && startsWith(matrix.arch, 'x86_64') && startsWith(matrix.compiler, 'cc') run: | msys2 -c './autogen.sh --enable-option-checking=fatal --enable-debug-messages --enable-tls-sigs --disable-npcap ${{ matrix.gcrypt }}' - - name: Build nDPI on Windows + - name: Build nDPI on Windows msys2 if: startsWith(matrix.os, 'windows') && startsWith(matrix.arch, 'x86_64') && startsWith(matrix.compiler, 'cc') run: | msys2 -c 'make all' diff --git a/.gitignore b/.gitignore index 1b4ccc971..74a832df7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,34 @@ +*.[o] +*.db +*.db-* +*.dll +*.ilk +*.in +*.ipch +*.ko +*.la +*.lo +*.log +*.obj +*.opendb +*.pdb +*.recipe +*.suo +*.swp +*.tlog +*.user +*o.cmd +*o.cmd *~ +.autotools +.cproject +.dirstamp +.libs +.project +.settings +/Makefile /aclocal.m4 /autom4te.cache/ -.autotools -compile /compile /config.guess /config.h @@ -12,72 +38,57 @@ compile /config.sub /configure /configure.tmp -.cproject /depcomp -.dirstamp +/doc/Doxyfile.cfg /example/.deps/ /example/Makefile -/example/ndpiReader /example/Makefile.dpdk /example/build +/example/libndpiReader.a +/example/ndpiReader +/example/ndpiReader.exe /example/ndpiSimpleIntegration -*.in +/fuzz/.deps/ +/fuzz/Makefile +/fuzz/fuzz_ndpi_reader +/fuzz/fuzz_ndpi_reader_with_main +/fuzz/fuzz_process_packet +/fuzz/fuzz_process_packet_with_main +/influxdb/Makefile /install-sh -*.ko -*.la /libndpi.pc -.libs /libtool -*.lo /ltmain.sh /m4/libtool.m4 -/m4/lt~obsolete.m4 /m4/ltoptions.m4 /m4/ltsugar.m4 /m4/ltversion.m4 -/Makefile +/m4/lt~obsolete.m4 /missing -*.[o] -*.obj -*o.cmd -*o.cmd -.project -.settings +/rrdtool/Makefile +/rrdtool/rrd_anomaly +/rrdtool/rrd_similarity /src/include/Makefile -/src/include/ndpi_config.h /src/include/ndpi_api.h +/src/include/ndpi_config.h /src/include/ndpi_define.h /src/lib/.deps/ /src/lib/Makefile +/src/lib/libndpi-*.dll +/src/lib/libndpi.4 +/src/lib/libndpi.a +/src/lib/libndpi.so* /src/lib/protocols/.deps/ /src/lib/third_party/src/.deps/ -stamp-h1 -*.swp +/tags /test-driver /tests/Makefile -/fuzz/.deps/ -/fuzz/Makefile -/fuzz/fuzz_ndpi_reader -/fuzz/fuzz_ndpi_reader_with_main -/fuzz/fuzz_process_packet -/fuzz/fuzz_process_packet_with_main /tests/dga/Makefile -/tests/do.sh -/tests/unit/Makefile -/example/libndpiReader.a -/example/ndpiReader.exe -/src/lib/libndpi.a -/src/lib/libndpi.so* -/src/lib/libndpi -/src/lib/libndpi-*.dll -/src/lib/libndpi.4 /tests/dga/dga_evaluate /tests/dga/dga_evaluate.exe -/tests/unit/unit -/tags -/rrdtool/Makefile -/rrdtool/rrd_anomaly -/rrdtool/rrd_similarity +/tests/do.sh /tests/performance/Makefile -/influxdb/Makefile -/doc/Doxyfile.cfg +/tests/unit/Makefile +/tests/unit/unit +compile +stamp-h1 diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c index d7de4e713..2dc39dd3c 100644 --- a/src/lib/ndpi_utils.c +++ b/src/lib/ndpi_utils.c @@ -1538,7 +1538,7 @@ int ndpi_flow2json(struct ndpi_detection_module_struct *ndpi_struct, u_int16_t src_port, u_int16_t dst_port, ndpi_protocol l7_protocol, ndpi_serializer *serializer) { - char src_name[32] = {}, dst_name[32] = {}; + char src_name[32] = {'\0'}, dst_name[32] = {'\0'}; if(ip_version == 4) { inet_ntop(AF_INET, &src_v4, src_name, sizeof(src_name)); diff --git a/src/lib/third_party/src/gcrypt/digest.c b/src/lib/third_party/src/gcrypt/digest.c index e68d905cf..48e6eea97 100644 --- a/src/lib/third_party/src/gcrypt/digest.c +++ b/src/lib/third_party/src/gcrypt/digest.c @@ -130,7 +130,7 @@ sha256_init(sha256_t *p) static void sha256_transform(uint32_t *state, const uint32_t *data) { - uint32_t W[16] = {}; + uint32_t W[16] = {0}; unsigned j; #ifdef _SHA256_UNROLL2 uint32_t a,b,c,d,e,f,g,h; diff --git a/src/lib/third_party/src/roaring.cc b/src/lib/third_party/src/roaring.cc index a2668fcda..36b96bed3 100644 --- a/src/lib/third_party/src/roaring.cc +++ b/src/lib/third_party/src/roaring.cc @@ -11983,7 +11983,7 @@ static void binarySearch2(const uint16_t *array, int32_t n, uint16_t target1, * and binarySearch2. This approach can be slightly superior to a conventional * galloping search in some instances. */ -static int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s, +static int32_t intersect_skewed_uint16(const uint16_t *small_set, size_t size_s, const uint16_t *large, size_t size_l, uint16_t *buffer) { size_t pos = 0, idx_l = 0, idx_s = 0; @@ -11993,10 +11993,10 @@ static int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s, } int32_t index1 = 0, index2 = 0, index3 = 0, index4 = 0; while ((idx_s + 4 <= size_s) && (idx_l < size_l)) { - uint16_t target1 = small[idx_s]; - uint16_t target2 = small[idx_s + 1]; - uint16_t target3 = small[idx_s + 2]; - uint16_t target4 = small[idx_s + 3]; + uint16_t target1 = small_set[idx_s]; + uint16_t target2 = small_set[idx_s + 1]; + uint16_t target3 = small_set[idx_s + 2]; + uint16_t target4 = small_set[idx_s + 3]; binarySearch4(large + idx_l, (int32_t)(size_l - idx_l), target1, target2, target3, target4, &index1, &index2, &index3, &index4); if ((index1 + idx_l < size_l) && (large[idx_l + index1] == target1)) { @@ -12015,8 +12015,8 @@ static int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s, idx_l += index4; } if ((idx_s + 2 <= size_s) && (idx_l < size_l)) { - uint16_t target1 = small[idx_s]; - uint16_t target2 = small[idx_s + 1]; + uint16_t target1 = small_set[idx_s]; + uint16_t target2 = small_set[idx_s + 1]; binarySearch2(large + idx_l, (int32_t)(size_l - idx_l), target1, target2, &index1, &index2); if ((index1 + idx_l < size_l) && (large[idx_l + index1] == target1)) { @@ -12029,7 +12029,7 @@ static int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s, idx_l += index2; } if ((idx_s < size_s) && (idx_l < size_l)) { - uint16_t val_s = small[idx_s]; + uint16_t val_s = small_set[idx_s]; int32_t index = binarySearch(large + idx_l, (int32_t)(size_l - idx_l), val_s); if (index >= 0) buffer[pos++] = val_s; @@ -12040,7 +12040,7 @@ static int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s, // TODO: this could be accelerated, possibly, by using binarySearch4 as above. -static int32_t intersect_skewed_uint16_cardinality(const uint16_t *small, +static int32_t intersect_skewed_uint16_cardinality(const uint16_t *small_set, size_t size_s, const uint16_t *large, size_t size_l) { @@ -12050,7 +12050,7 @@ static int32_t intersect_skewed_uint16_cardinality(const uint16_t *small, return 0; } - uint16_t val_l = large[idx_l], val_s = small[idx_s]; + uint16_t val_l = large[idx_l], val_s = small_set[idx_s]; while (true) { if (val_l < val_s) { @@ -12060,12 +12060,12 @@ static int32_t intersect_skewed_uint16_cardinality(const uint16_t *small, } else if (val_s < val_l) { idx_s++; if (idx_s == size_s) break; - val_s = small[idx_s]; + val_s = small_set[idx_s]; } else { pos++; idx_s++; if (idx_s == size_s) break; - val_s = small[idx_s]; + val_s = small_set[idx_s]; idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s); if (idx_l == size_l) break; val_l = large[idx_l]; @@ -12075,7 +12075,7 @@ static int32_t intersect_skewed_uint16_cardinality(const uint16_t *small, return (int32_t)pos; } -bool intersect_skewed_uint16_nonempty(const uint16_t *small, size_t size_s, +bool intersect_skewed_uint16_nonempty(const uint16_t *small_set, size_t size_s, const uint16_t *large, size_t size_l) { size_t idx_l = 0, idx_s = 0; @@ -12083,7 +12083,7 @@ bool intersect_skewed_uint16_nonempty(const uint16_t *small, size_t size_s, return false; } - uint16_t val_l = large[idx_l], val_s = small[idx_s]; + uint16_t val_l = large[idx_l], val_s = small_set[idx_s]; while (true) { if (val_l < val_s) { @@ -12093,7 +12093,7 @@ bool intersect_skewed_uint16_nonempty(const uint16_t *small, size_t size_s, } else if (val_s < val_l) { idx_s++; if (idx_s == size_s) break; - val_s = small[idx_s]; + val_s = small_set[idx_s]; } else { return true; } diff --git a/windows/nDPI.sln b/windows/nDPI.sln new file mode 100644 index 000000000..e98141873 --- /dev/null +++ b/windows/nDPI.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32126.315 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "nDPI", "nDPI.vcxproj", "{7331961B-A2B5-45B1-B144-AFDEFBB0EB01}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7331961B-A2B5-45B1-B144-AFDEFBB0EB01}.Debug|x64.ActiveCfg = Debug|x64 + {7331961B-A2B5-45B1-B144-AFDEFBB0EB01}.Debug|x64.Build.0 = Debug|x64 + {7331961B-A2B5-45B1-B144-AFDEFBB0EB01}.Release|x64.ActiveCfg = Release|x64 + {7331961B-A2B5-45B1-B144-AFDEFBB0EB01}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {ED429DB6-BA49-4281-943C-0E57E33B5735} + EndGlobalSection +EndGlobal diff --git a/windows/nDPI.vcxproj b/windows/nDPI.vcxproj new file mode 100644 index 000000000..9686f2be6 --- /dev/null +++ b/windows/nDPI.vcxproj @@ -0,0 +1,454 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {7331961B-A2B5-45B1-B144-AFDEFBB0EB01} + nDPIdev + Win32Proj + nDPI + 8.1 + + + + DynamicLibrary + v140 + true + Unicode + + + DynamicLibrary + v142 + true + Unicode + + + StaticLibrary + v140 + MultiByte + + + DynamicLibrary + v142 + Unicode + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>11.0.50727.1 + + + $(ProjectDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)obj\$(ProjectName)_$(Configuration)_$(Platform)\ + true + + + true + $(SolutionDir)obj\$(ProjectName)_$(Configuration)_$(Platform)\ + $(ProjectDir)bin\$(Platform)_$(Configuration)\ + $(ProjectDir);$(ProjectDir)dummy\folder\;$(IncludePath) + + + $(ProjectDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)obj\$(ProjectName)_$(Configuration)_$(Platform)\ + false + $(ProjectDir);$(ProjectDir)dummy\folder\;$(IncludePath) + + + false + $(SolutionDir)obj\$(ProjectName)_$(Configuration)_$(Platform)\ + $(ProjectDir)bin\$(Platform)_$(Configuration)\ + $(ProjectDir);$(ProjectDir)dummy\folder\;$(IncludePath) + + + + Disabled + $(ProjectDir)..\src\lib\protocols\;$(ProjectDir)..\src\include\;$(ProjectDir)..\;$(ProjectDir)..\src\lib\third_party\include\;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + false + Default + MultiThreadedDebugDLL + + Level3 + ProgramDatabase + false + $(TargetDir)$(TargetName).pdb + false + true + + + true + Windows + MachineX86 + Ws2_32.lib;%(AdditionalDependencies) + true + true + + + + + Disabled + $(ProjectDir)src\;$(ProjectDir)..\src\lib\protocols\;$(ProjectDir)..\src\include\;$(ProjectDir)..\;$(ProjectDir)..\src\lib\third_party\include\;%(AdditionalIncludeDirectories) + NDPI_LIB_COMPILATION;WIN32;_WIN64;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) + Default + MultiThreadedDebugDLL + + + Level3 + ProgramDatabase + false + $(TargetDir)$(TargetName).pdb + false + true + $(ProjectDir);%(AdditionalUsingDirectories) + Default + + + true + Windows + Ws2_32.lib;%(AdditionalDependencies) + true + true + + + + + + + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + MultiThreaded + + TurnOffAllWarnings + None + $(ProjectDir)..\src\lib\protocols\;$(ProjectDir)..\src\include\;$(ProjectDir)..\;$(ProjectDir)..\src\lib\third_party\include\;%(AdditionalIncludeDirectories) + Full + AnySuitable + true + Speed + true + true + true + $(TargetDir)$(TargetName).pdb + true + $(ProjectDir);%(AdditionalUsingDirectories) + true + Sync + + + false + Console + true + true + MachineX86 + Ws2_32.lib;%(AdditionalDependencies) + + + + + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreaded + + + TurnOffAllWarnings + None + $(ProjectDir)..\src\lib\protocols\;$(ProjectDir)..\src\include\;$(ProjectDir)..\;$(ProjectDir)..\src\lib\third_party\include\;%(AdditionalIncludeDirectories) + Full + AnySuitable + true + Speed + true + true + true + $(TargetDir)$(TargetName).pdb + true + $(ProjectDir);%(AdditionalUsingDirectories) + Sync + true + Default + + + false + Console + true + true + Ws2_32.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/windows/nDPI.vcxproj.filters b/windows/nDPI.vcxproj.filters new file mode 100644 index 000000000..f57d96502 --- /dev/null +++ b/windows/nDPI.vcxproj.filters @@ -0,0 +1,298 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + third_party + + + + + + + + {51f8ee9e-a41d-4343-8115-937ed5a1ea19} + + + \ No newline at end of file diff --git a/windows/src/getopt.c b/windows/src/getopt.c new file mode 100644 index 000000000..fbd86104a --- /dev/null +++ b/windows/src/getopt.c @@ -0,0 +1,117 @@ +// Put this in a separate .h file (called "getopt.h"). +// The prototype for the header file is: +/* +#ifndef GETOPT_H +#define GETOPT_H + +int getopt(int nargc, char * const nargv[], const char *ostr) ; + +#endif +*/ + +#include "getopt.h" // make sure you construct the header file as dictated above + +/* +* Copyright (c) 1987, 1993, 1994 +* The Regents of the University of California. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. All advertising materials mentioning features or use of this software +* must display the following acknowledgement: +* This product includes software developed by the University of +* California, Berkeley and its contributors. +* 4. Neither the name of the University nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 +#include + +int opterr = 1, /* if error message should be printed */ +optind = 1, /* index into parent argv vector */ +optopt, /* character checked for validity */ +optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ + +#define BADCH (int)'?' +#define BADARG (int)':' +#define EMSG "" + + /* + * getopt -- + * Parse argc/argv argument vector. + */ +int getopt(int nargc, char * const nargv[], const char *ostr) +{ + static char *place = EMSG; /* option letter processing */ + const char *oli; /* option letter list index */ + + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc || *(place = nargv[optind]) != '-') { + place = EMSG; + return (-1); + } + if (place[1] && *++place == '-') { /* found "--" */ + ++optind; + place = EMSG; + return (-1); + } + } /* option letter okay? */ + if ((optopt = (int)*place++) == (int)':' || + !(oli = strchr(ostr, optopt))) { + /* + * if the user didn't specify '-' as an option, + * assume it means -1. + */ + if (optopt == (int)'-') + return (-1); + if (!*place) + ++optind; + if (opterr && *ostr != ':') + (void)printf("illegal option -- %c\n", optopt); + return (BADCH); + } + if (*++oli != ':') { /* don't need argument */ + optarg = NULL; + if (!*place) + ++optind; + } + else { /* need an argument */ + if (*place) /* no white space */ + optarg = place; + else if (nargc <= ++optind) { /* no arg */ + place = EMSG; + if (*ostr == ':') + return (BADARG); + if (opterr) + (void)printf("option requires an argument -- %c\n", optopt); + return (BADCH); + } + else /* white space */ + optarg = nargv[optind]; + place = EMSG; + ++optind; + } + return (optopt); /* dump back option letter */ +} diff --git a/windows/src/getopt.h b/windows/src/getopt.h new file mode 100644 index 000000000..eb418c3ab --- /dev/null +++ b/windows/src/getopt.h @@ -0,0 +1,6 @@ +#ifndef GETOPT_H +#define GETOPT_H + +int getopt(int nargc, char * const nargv[], const char *ostr) ; + +#endif diff --git a/windows/src/win-gettimeofday.c b/windows/src/win-gettimeofday.c new file mode 100644 index 000000000..c921b3325 --- /dev/null +++ b/windows/src/win-gettimeofday.c @@ -0,0 +1,55 @@ +/* +* Author: Ugo Varetto - ugovaretto@gmail.com +* This code is distributed under the terms of the Apache Software License version 2.0 +* https://opensource.org/licenses/Apache-2.0 +*/ + +#include +#include + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag = 0; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + tmpres /= 10; /*convert into microseconds*/ + /*converting file time to unix epoch*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} -- cgit v1.2.3