diff --git a/miniupnpc/connecthostport.c b/miniupnpc/connecthostport.c index a798b38..61ff491 100644 --- a/miniupnpc/connecthostport.c +++ b/miniupnpc/connecthostport.c @@ -19,11 +19,7 @@ #include #include #define MAXHOSTNAMELEN 64 -/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */ -#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO)) -/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */ -#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__))) -#endif +#include "win32_snprintf.h" #define herror #define socklen_t int #else /* #ifdef _WIN32 */ diff --git a/miniupnpc/minisoap.c b/miniupnpc/minisoap.c index 55d3e76..9de3746 100644 --- a/miniupnpc/minisoap.c +++ b/miniupnpc/minisoap.c @@ -13,11 +13,7 @@ #ifdef _WIN32 #include #include -/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */ -#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO)) -/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */ -#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__))) -#endif +#include "win32_snprintf.h" #else #include #include diff --git a/miniupnpc/minissdpc.c b/miniupnpc/minissdpc.c index 048cd25..1e99a04 100644 --- a/miniupnpc/minissdpc.c +++ b/miniupnpc/minissdpc.c @@ -20,11 +20,7 @@ #include #include #include -/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */ -#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO)) -/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */ -#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__))) -#endif +#include "win32_snprintf.h" #if !defined(_MSC_VER) #include #else /* !defined(_MSC_VER) */ diff --git a/miniupnpc/miniupnpc.c b/miniupnpc/miniupnpc.c index b11760e..1c764c7 100644 --- a/miniupnpc/miniupnpc.c +++ b/miniupnpc/miniupnpc.c @@ -15,11 +15,7 @@ #include #include #include -/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */ -#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO)) -/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */ -#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__))) -#endif +#include "win32_snprintf.h" #define strdup _strdup #ifndef strncasecmp #if defined(_MSC_VER) && (_MSC_VER >= 1400) diff --git a/miniupnpc/miniwget.c b/miniupnpc/miniwget.c index 6d148c6..d5b7970 100644 --- a/miniupnpc/miniwget.c +++ b/miniupnpc/miniwget.c @@ -15,11 +15,7 @@ #include #include #define MAXHOSTNAMELEN 64 -/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */ -#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO)) -/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */ -#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__))) -#endif +#include "win32_snprintf.h" #define socklen_t int #ifndef strncasecmp #if defined(_MSC_VER) && (_MSC_VER >= 1400) diff --git a/miniupnpc/msvc/miniupnpc.vcxproj b/miniupnpc/msvc/miniupnpc.vcxproj index d91271f..cb4defa 100644 --- a/miniupnpc/msvc/miniupnpc.vcxproj +++ b/miniupnpc/msvc/miniupnpc.vcxproj @@ -183,6 +183,7 @@ + diff --git a/miniupnpc/msvc/miniupnpc.vcxproj.filters b/miniupnpc/msvc/miniupnpc.vcxproj.filters index 6f53c81..c79060a 100644 --- a/miniupnpc/msvc/miniupnpc.vcxproj.filters +++ b/miniupnpc/msvc/miniupnpc.vcxproj.filters @@ -113,6 +113,9 @@ Fichiers d%27en-tête + + Fichiers d%27en-tête + diff --git a/miniupnpc/msvc/miniupnpc_vs2010.vcxproj b/miniupnpc/msvc/miniupnpc_vs2010.vcxproj index 7a3156e..b97c370 100644 --- a/miniupnpc/msvc/miniupnpc_vs2010.vcxproj +++ b/miniupnpc/msvc/miniupnpc_vs2010.vcxproj @@ -179,6 +179,7 @@ + diff --git a/miniupnpc/msvc/miniupnpc_vs2010.vcxproj.filters b/miniupnpc/msvc/miniupnpc_vs2010.vcxproj.filters index d52bb8b..1ab34f9 100644 --- a/miniupnpc/msvc/miniupnpc_vs2010.vcxproj.filters +++ b/miniupnpc/msvc/miniupnpc_vs2010.vcxproj.filters @@ -102,6 +102,9 @@ headers + + Fichiers d%27en-tête + diff --git a/miniupnpc/upnpc.c b/miniupnpc/upnpc.c index 6ec0ad8..11c308d 100644 --- a/miniupnpc/upnpc.c +++ b/miniupnpc/upnpc.c @@ -11,11 +11,7 @@ #include #ifdef _WIN32 #include -/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */ -#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO)) -/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */ -#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__))) -#endif +#include "win32_snprintf.h" #else /* for IPPROTO_TCP / IPPROTO_UDP */ #include diff --git a/miniupnpc/win32_snprintf.h b/miniupnpc/win32_snprintf.h new file mode 100644 index 0000000..1fc284e --- /dev/null +++ b/miniupnpc/win32_snprintf.h @@ -0,0 +1,71 @@ +/* vim: tabstop=4 shiftwidth=4 noexpandtab + * MiniUPnP project + * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ + * (c) 2020 Pali Rohár + * This software is subject to the conditions detailed + * in the LICENCE file provided within the distribution */ + +#ifndef WIN32_SNPRINTF_H +#define WIN32_SNPRINTF_H + +#ifdef _WIN32 + +#include + +/* snprintf is supported by: + * - Visual Studio 2015 or new + * - mingw32 with iso c ext + * - mingw-w64 with ansi stdio + * - mingw-w64 6.0.0 or new with ucrt + * - mingw-w64 8.0.0 or new with iso c ext + */ +#if ( \ + (defined(_MSC_VER) && _MSC_VER < 1900) /* Visual Studio older than 2015 */ || \ + (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && defined(__NO_ISOCEXT)) /* mingw32 without iso c ext */ || \ + (defined(__MINGW64_VERSION_MAJOR) && /* mingw-w64 not ... */ !( \ + (defined (__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO != 0)) /* ... with ansi stdio */ || \ + (__MINGW64_VERSION_MAJOR >= 6 && defined(_UCRT)) /* ... at least 6.0.0 with ucrt */ || \ + (__MINGW64_VERSION_MAJOR >= 8 && !defined(__NO_ISOCEXT)) /* ... at least 8.0.0 with iso c ext */ || \ + 0) || \ +0) + +/* _scprintf is supported by: + * - Visual Studio 2002 or new + * - msvcr70.dll or new + * - msvcrt.dll on Windows XP or new + */ +#if ( \ + (defined(_MSC_VER) && _MSC_VER < 1300) /* Visual Studio older than 2002 */ || \ + (defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x700) /* msvcrt older than 7.0 */ || \ +0) +#define CHECK_SCPRINTF 0 +#define IF_SCPRINTF(expr) 0 +#define ELSE_SCPRINTF(expr) expr +#else +#define CHECK_SCPRINTF 1 +#define IF_SCPRINTF(expr) expr +#define ELSE_SCPRINTF(expr) 0 +#endif + +/* Emulation of snprintf for win32 */ +#define snprintf(buf, size, fmt, ...) ( \ + (((size) != 0 && (buf) != NULL) ? ( /* _snprintf does not work with NULL buffer */ \ + _snprintf((buf), (size), (fmt), __VA_ARGS__), /* _snprintf returns -1 on overflow, so ignore its value */ \ + (((char *)buf)[(size_t)(size)-1] = 0), /* _snprintf does not fill nul byte on overflow */ \ + 0) : 0), \ + (CHECK_SCPRINTF ? IF_SCPRINTF( \ + _scprintf((fmt), __VA_ARGS__) /* calculate return value for snprintf via _scprintf */ \ + ) : ELSE_SCPRINTF( \ + ((size) != 0 && (buf) != NULL) ? \ + strlen((buf)) /* return just length of buffer */ \ + : \ + 1 /* no buffer, impossible to calculate, return just non-zero number */ \ + ) \ + ) \ +) + +#endif + +#endif /* _WIN32 */ + +#endif /* WIN32_SNPRINTF_H */