language level macro cleanup; don't roll our own dbstrings when that's avoidable

This commit is contained in:
Brian Cox 2019-09-29 14:40:58 -07:00
parent 70cd4ab27d
commit c1d21ff630
18 changed files with 99 additions and 42 deletions

View File

@ -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<dbchar_t> 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 <locale>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
}
}

View File

@ -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

View File

@ -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 <stdint.h>
#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;

View File

@ -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<int, int>(std::isspace))));
#else
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int c) {return !std::isspace(c);} ));

View File

@ -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] );

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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]);

View File

@ -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<int, int>(std::isspace)), strPropList.end());
#else

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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);
}