From c1d21ff630a69b82fa5023f0325a45885ed17088 Mon Sep 17 00:00:00 2001 From: Brian Cox Date: Sun, 29 Sep 2019 14:40:58 -0700 Subject: [PATCH] language level macro cleanup; don't roll our own dbstrings when that's avoidable --- src/core/ntdbs.h | 8 +++++++- src/core/ntmbs.h | 5 ++++- src/core/platform.h | 22 ++++++++++++++++++---- src/core/types.h | 2 +- src/core/wchar16.cpp | 20 ++++++++++++++++++++ src/core/wchar16.h | 25 ++++++++++++++++++++++++- src/cryptlib/config.h | 8 ++++---- src/fs/fsparserutil.cpp | 2 +- src/siggen/siggenmain.cpp | 4 ++-- src/tripwire/tripwiremain.cpp | 4 ++-- src/tw/twutil.cpp | 16 ++++------------ src/twadmin/keygeneration.cpp | 4 +--- src/twadmin/twadminmain.cpp | 4 ++-- src/twparser/parserhelper.cpp | 2 +- src/twprint/twprintmain.cpp | 4 ++-- src/twtest/charutil_t.cpp | 2 +- src/twtest/test.cpp | 4 ++-- src/twtest/wchar16_t.cpp | 5 +++-- 18 files changed, 99 insertions(+), 42 deletions(-) diff --git a/src/core/ntdbs.h b/src/core/ntdbs.h index 2269ff4..8d55ce0 100644 --- a/src/core/ntdbs.h +++ b/src/core/ntdbs.h @@ -71,6 +71,12 @@ namespace tss typedef std::wstring dbstring; } +#elif (USE_U16STRING) +namespace tss +{ +typedef std::u16string dbstring; +} + #elif (WCHAR_IS_32_BITS) namespace std { @@ -87,7 +93,7 @@ typedef std::basic_string dbstring; #endif -#if WCHAR_IS_32_BITS // We already have a dbstring implicitly in wstring!!! +#if NEED_DBSTRING_IMPL // We already have a dbstring implicitly in wstring!!! #if HAVE_LOCALE # include diff --git a/src/core/ntmbs.h b/src/core/ntmbs.h index ba2377b..fc49104 100644 --- a/src/core/ntmbs.h +++ b/src/core/ntmbs.h @@ -86,9 +86,12 @@ typedef const wchar_t* const_ntwcs_t; # define NTDBS_T_DEFINED # if WCHAR_IS_16_BITS typedef wchar_t dbchar_t; // Same size but use NT's type +# elif USE_CHAR16_T +typedef char16_t dbchar_t; # else -typedef uint16_t dbchar_t; +typedef uint16_t dbchar_t; # endif + typedef dbchar_t* ntdbs_t; typedef const dbchar_t* const_ntdbs_t; #endif //NTDBS_T_DEFINED diff --git a/src/core/platform.h b/src/core/platform.h index 2e01f52..2fe64a9 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -389,6 +389,11 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# define CPLUSPLUS_2011_OR_GREATER (__cplusplus >= 201103L) +# define CPLUSPLUS_PRE_2011 !CPLUSPLUS_2011_OR_GREATER + +# define CPLUSPLUS_2017_OR_GREATER (__cplusplus >= 201703L) +# define CPLUSPLUS_PRE_2017 !CPLUSPLUS_2011_OR_GREATER // KAI 3.4 uses a much improved stl # define IS_KAI_3_4 (IS_KAI && (COMP == COMP_KAI_IRIX || COMP == COMP_KAI_OSF1ALPHA || COMP == COMP_KAI_GLIBC)) @@ -420,18 +425,27 @@ # define WCHAR_IS_32_BITS 1 # endif +# define USE_U16STRING ((!WCHAR_IS_16_BITS) && CPLUSPLUS_2011_OR_GREATER) +# define USE_CHAR16_T USE_U16STRING +# define NEED_DBSTRING_IMPL (!WCHAR_IS_16_BITS && !USE_U16STRING) + +# define USE_UNIQUE_PTR CPLUSPLUS_2011_OR_GREATER +# define USE_LAMBDAS CPLUSPLUS_2011_OR_GREATER +# define USE_UNICODE_ESCAPES CPLUSPLUS_2011_OR_GREATER +# define USE_UNEXPECTED CPLUSPLUS_PRE_2017 + # define SUPPORTS_POSIX_FORK_EXEC (HAVE_FORK && HAVE_EXECVE) // msystem+mpopen fail on Syllable, so use the libc equivalents until we figure out why. // TODO: Figure out why. # define USES_MPOPEN (SUPPORTS_POSIX_FORK_EXEC && !IS_SYLLABLE) # define USES_MSYSTEM (SUPPORTS_POSIX_FORK_EXEC && !IS_SYLLABLE) -# define SUPPORTS_WCHART IS_WIN32 // TODO: Remove after getting new ver of KAI +//# define SUPPORTS_WCHART IS_WIN32 // TODO: Remove this? # define USES_GLIBC ((COMP == COMP_KAI_GLIBC) || HAVE_GCC) # define SUPPORTS_MEMBER_TEMPLATES (!IS_SUNPRO) # define SUPPORTS_EXPLICIT_TEMPLATE_FUNC_INST (!IS_SUNPRO) -# define SUPPORTS_POSIX_SIGNALS (!IS_DOS_DJGPP) +# define SUPPORTS_POSIX_SIGNALS (!IS_DOS_DJGPP && !IS_MINGW) # define SUPPORTS_NETWORKING (HAVE_SOCKET && !IS_SORTIX && !IS_DOS_DJGPP && !IS_REDOX) # define SUPPORTS_SYSLOG (HAVE_SYSLOG && !IS_SKYOS && !IS_RISCOS) # define NEEDS_SWAB_IMPL (IS_CYGWIN || IS_SYLLABLE || IS_ANDROID || IS_SORTIX) @@ -492,9 +506,9 @@ // Work around single-arg mkdir on MinGW. // consider using autoconf AX_FUNC_MKDIR if // we need to handle any more cases here -#if IS_MINGW +/*#if IS_MINGW # define mkdir(a,b) mkdir(a) -#endif +#endif*/ //============================================================================= // Miscellaneous diff --git a/src/core/types.h b/src/core/types.h index 98e40ac..ffbcc4f 100644 --- a/src/core/types.h +++ b/src/core/types.h @@ -187,7 +187,7 @@ inline int64_t SWAPBYTES64(int64_t i) //////////////////////////////////////////// -# if __cplusplus >= 201103L +# if USE_UNIQUE_PTR # define TW_UNIQUE_PTR std::unique_ptr # else # define TW_UNIQUE_PTR std::auto_ptr diff --git a/src/core/wchar16.cpp b/src/core/wchar16.cpp index b28c315..36545ae 100644 --- a/src/core/wchar16.cpp +++ b/src/core/wchar16.cpp @@ -52,6 +52,7 @@ int wchar16len(const WCHAR16* s) return i; } +#if NEED_DBSTRING_IMPL //============================================================================= // class wc16_string // @@ -331,3 +332,22 @@ void wc16_string_impl::CopyString(const wc16_string_impl& rhs) memcpy(this->pString, rhs.pString, newlen * sizeof(WCHAR16)); } +#endif + +namespace tss +{ +void swapbytes(wc16_string& str) +{ +#if NEED_DBSTRING_IMPL + str.swapbytes(); +#else + size_t len = str.length(); + for (size_t x=0; x < len; x++) + { + WCHAR16 current = str[x]; + str[x] = ((current >> 8) | ((current & 0xFF) << 8)); + } +#endif +} +} + diff --git a/src/core/wchar16.h b/src/core/wchar16.h index 8b6366a..5d93d02 100644 --- a/src/core/wchar16.h +++ b/src/core/wchar16.h @@ -38,11 +38,22 @@ #ifndef __WCHAR16_H #define __WCHAR16_H +#include "platform.h" + // TODO: Perhaps WCHAR16 should come out of types.h??? #ifndef __TYPES_H #include "types.h" #endif +#if WCHAR_IS_16_BITS +typedef wchar_t WCHAR16; +typedef std::wstring wc16_string; + +#elif USE_U16STRING +typedef char16_t WCHAR16; +typedef std::u16string wc16_string; + +#else typedef uint16_t WCHAR16; // unix has 4 byte wchar_t, but we want to standardize on 16 bit wide chars //============================================================================= @@ -107,5 +118,17 @@ public: private: wc16_string_impl* mpData; }; - #endif + +namespace tss +{ + void swapbytes(wc16_string& str); +} + +#ifndef WORDS_BIGENDIAN +#define TSS_SwapBytes(x) tss::swapbytes(x) +#else +#define TSS_SwapBytes(x) +#endif + +#endif // __WCHAR16_H diff --git a/src/cryptlib/config.h b/src/cryptlib/config.h index 8bca8a7..fa098e9 100644 --- a/src/cryptlib/config.h +++ b/src/cryptlib/config.h @@ -86,13 +86,13 @@ // Make sure these typedefs are correct for your computer -#if __cplusplus < 201103L +#if CPLUSPLUS_PRE_2011 typedef unsigned char uint8_t; #else #include #endif -#if __cplusplus < 201103L +#if CPLUSPLUS_PRE_2011 typedef unsigned short word16; #if SIZEOF_INT == 4 typedef unsigned int word32; @@ -155,7 +155,7 @@ typedef unsigned long long word64; #elif defined(__GNUC__) -#if __cplusplus < 201103L +#if CPLUSPLUS_PRE_2011 typedef word32 word; #if SIZEOF_LONG_LONG == 8 typedef unsigned long long dword; @@ -165,7 +165,7 @@ typedef word32 word; #else #error "I don't seem to have a 64-bit integer type on this system." #endif -#else // __cplusplus < 201103L +#else // CPLUSPLUS_PRE_2011 typedef uint32_t word; typedef uint64_t dword; diff --git a/src/fs/fsparserutil.cpp b/src/fs/fsparserutil.cpp index 75d6acf..2b09e2a 100644 --- a/src/fs/fsparserutil.cpp +++ b/src/fs/fsparserutil.cpp @@ -179,7 +179,7 @@ static inline void trim_leading_whitespace(std::string& str) { // C++17 removes std::ptr_fun and deprecates std::not1, // so just use a lambda where available -#if __cplusplus < 201103L +#if !USE_LAMBDAS str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun(std::isspace)))); #else str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int c) {return !std::isspace(c);} )); diff --git a/src/siggen/siggenmain.cpp b/src/siggen/siggenmain.cpp index e309329..a9df80f 100644 --- a/src/siggen/siggenmain.cpp +++ b/src/siggen/siggenmain.cpp @@ -71,7 +71,7 @@ void tw_terminate_handler() #endif } -#if __cplusplus < 201703L +#if USE_UNEXPECTED void tw_unexpected_handler() { fputs("### Internal Error.\n### Unexpected Exception Handler called.\n### Exiting...\n", stderr); @@ -122,7 +122,7 @@ int __cdecl _tmain(int argc, const TCHAR** argv) // Note: we do this before Init() in case it attempts to call these handlers // TODO: move this into the Init() routine EXCEPTION_NAMESPACE set_terminate(tw_terminate_handler); -#if __cplusplus < 201703L +#if USE_UNEXPECTED EXCEPTION_NAMESPACE set_unexpected(tw_unexpected_handler); #endif //cTWInit twInit( argv[0] ); diff --git a/src/tripwire/tripwiremain.cpp b/src/tripwire/tripwiremain.cpp index 4decfc8..9613778 100644 --- a/src/tripwire/tripwiremain.cpp +++ b/src/tripwire/tripwiremain.cpp @@ -99,7 +99,7 @@ void tw_terminate_handler() #endif } -#if __cplusplus < 201703L +#if USE_UNEXPECTED void tw_unexpected_handler() { fputs("### Internal Error.\n### Unexpected Exception Handler called.\n### Exiting...\n", stderr); @@ -132,7 +132,7 @@ int __cdecl _tmain(int argc, const TCHAR* argv[], const TCHAR* envp[]) // Note: we do this before Init() in case it attempts to call these handlers // TODO: move this into the Init() routine EXCEPTION_NAMESPACE set_terminate(tw_terminate_handler); -#if __cplusplus < 201703L +#if USE_UNEXPECTED EXCEPTION_NAMESPACE set_unexpected(tw_unexpected_handler); #endif // Initialization diff --git a/src/tw/twutil.cpp b/src/tw/twutil.cpp index cc38567..7906e69 100644 --- a/src/tw/twutil.cpp +++ b/src/tw/twutil.cpp @@ -762,9 +762,7 @@ cTWUtil::CreatePrivateKey(cKeyFile& keyFile, const WCHAR16* usePassphrase, KeyTy passphrase = usePassphrase; -#ifndef WORDS_BIGENDIAN - passphrase.swapbytes(); -#endif + TSS_SwapBytes(passphrase); pPrivateKey = keyFile.GetPrivateKey((int8_t*)passphrase.data(), passphrase.length() * sizeof(WCHAR16)); @@ -804,9 +802,7 @@ cTWUtil::CreatePrivateKey(cKeyFile& keyFile, const WCHAR16* usePassphrase, KeyTy // sleep to hinder brute force (dictionary, etc.) attacks iFSServices::GetInstance()->Sleep(nSecs); -#ifndef WORDS_BIGENDIAN - passphrase.swapbytes(); -#endif + TSS_SwapBytes(passphrase); pPrivateKey = keyFile.GetPrivateKey((int8_t*)passphrase.data(), passphrase.length() * sizeof(WCHAR16)); @@ -840,9 +836,7 @@ void cTWUtil::CreatePrivateKey( passphrase = usePassphrase; -#ifndef WORDS_BIGENDIAN - passphrase.swapbytes(); -#endif + TSS_SwapBytes(passphrase); if (proxy.AquireKey(keyFile, (int8_t*)passphrase.data(), passphrase.length() * sizeof(WCHAR16))) return; @@ -880,9 +874,7 @@ void cTWUtil::CreatePrivateKey( // sleep to hinder brute force (dictionary, etc.) attacks iFSServices::GetInstance()->Sleep(nSecs); -#ifndef WORDS_BIGENDIAN - passphrase.swapbytes(); -#endif + TSS_SwapBytes(passphrase); if (proxy.AquireKey(keyFile, (int8_t*)passphrase.data(), passphrase.length() * sizeof(WCHAR16))) return; diff --git a/src/twadmin/keygeneration.cpp b/src/twadmin/keygeneration.cpp index f27f6d0..9d26a0a 100644 --- a/src/twadmin/keygeneration.cpp +++ b/src/twadmin/keygeneration.cpp @@ -130,9 +130,7 @@ static void GeneratePublicPrivateKeys(void* pParams, const cElGamalSig::KeySize bool GenerateKey(const TCHAR* keyPath, wc16_string passphrase, const cElGamalSig::KeySize key_size) { -#ifndef WORDS_BIGENDIAN - passphrase.swapbytes(); -#endif + TSS_SwapBytes(passphrase); #ifdef DEBUG // test reading in the keys diff --git a/src/twadmin/twadminmain.cpp b/src/twadmin/twadminmain.cpp index 7045b55..3e0750c 100644 --- a/src/twadmin/twadminmain.cpp +++ b/src/twadmin/twadminmain.cpp @@ -66,7 +66,7 @@ void tw_terminate_handler() #endif } -#if __cplusplus < 201703L +#if USE_UNEXPECTED void tw_unexpected_handler() { fputs("### Internal Error.\n### Unexpected Exception Handler called.\n### Exiting...\n", stderr); @@ -95,7 +95,7 @@ int __cdecl _tmain(int argc, const TCHAR* argv[], const TCHAR* envp[]) // Note: we do this before Init() in case it attempts to call these handlers // TODO: move this into the Init() routine EXCEPTION_NAMESPACE set_terminate(tw_terminate_handler); -#if __cplusplus < 201703L +#if USE_UNEXPECTED EXCEPTION_NAMESPACE set_unexpected(tw_unexpected_handler); #endif twInit.Init(argv[0]); diff --git a/src/twparser/parserhelper.cpp b/src/twparser/parserhelper.cpp index ed2911c..2be1284 100644 --- a/src/twparser/parserhelper.cpp +++ b/src/twparser/parserhelper.cpp @@ -681,7 +681,7 @@ void cParserUtil::CreatePropVector(const TSTRING& strPropListC, class cFCOPropVe TSTRING strPropList = strPropListC; // C++17 removes std::ptr_fun, so use a lambda where available -#if __cplusplus < 201103L +#if !USE_LAMBDAS strPropList.erase(std::remove_if(strPropList.begin(), strPropList.end(), std::ptr_fun(std::isspace)), strPropList.end()); #else diff --git a/src/twprint/twprintmain.cpp b/src/twprint/twprintmain.cpp index 351a10a..61f4744 100644 --- a/src/twprint/twprintmain.cpp +++ b/src/twprint/twprintmain.cpp @@ -75,7 +75,7 @@ void tw_terminate_handler() } // Exception specifications removed as a misfeature in C++17 -#if __cplusplus < 201703L +#if USE_UNEXPECTED void tw_unexpected_handler() { fputs("### Internal Error.\n### Unexpected Exception Handler called.\n### Exiting...\n", stderr); @@ -105,7 +105,7 @@ int __cdecl _tmain(int argc, const TCHAR* argv[]) // TODO: move this into the Init() routine EXCEPTION_NAMESPACE set_terminate(tw_terminate_handler); -#if __cplusplus < 201703L +#if USE_UNEXPECTED EXCEPTION_NAMESPACE set_unexpected(tw_unexpected_handler); #endif diff --git a/src/twtest/charutil_t.cpp b/src/twtest/charutil_t.cpp index 483319f..3b34d49 100644 --- a/src/twtest/charutil_t.cpp +++ b/src/twtest/charutil_t.cpp @@ -84,7 +84,7 @@ void TestCharUtilBasic() CheckChars("fo\x23 54"); // Test case requires support for Unicode escape sequences -#if __cplusplus >= 201103L +#if USE_UNICODE_ESCAPES if (localeIsUtf8()) CheckChars("\U0001F408", 4); //Cat emoji, if UTF-8 else diff --git a/src/twtest/test.cpp b/src/twtest/test.cpp index 1241454..ebfbef5 100644 --- a/src/twtest/test.cpp +++ b/src/twtest/test.cpp @@ -431,7 +431,7 @@ void tw_terminate_handler() exit(1); } -#if __cplusplus < 201703L +#if USE_UNEXPECTED void tw_unexpected_handler() { fputs("### Internal Error.\n### Unexpected Exception Handler called.\n### Exiting...\n", stderr); @@ -451,7 +451,7 @@ int _tmain(int argc, TCHAR** argv) try { EXCEPTION_NAMESPACE set_terminate(tw_terminate_handler); -#if __cplusplus < 201703L +#if USE_UNEXPECTED EXCEPTION_NAMESPACE set_unexpected(tw_unexpected_handler); #endif if (argc < 2) diff --git a/src/twtest/wchar16_t.cpp b/src/twtest/wchar16_t.cpp index 31a3195..2aec6f7 100644 --- a/src/twtest/wchar16_t.cpp +++ b/src/twtest/wchar16_t.cpp @@ -116,9 +116,9 @@ void TestWchar16() } #endif - b.swapbytes(); + tss::swapbytes(b); TEST(memcmp(a.data(), STRING1, 4) == 0); - b.swapbytes(); + tss::swapbytes(b); TEST(memcmp(b.data(), STRING1, 4) == 0); //#endif // IS_UNIX @@ -129,3 +129,4 @@ void RegisterSuite_Wchar16() { RegisterTest("Wchar16", "Basic", TestWchar16); } +