Skip to content

Commit

Permalink
Reduce C++ file scope class objects
Browse files Browse the repository at this point in the history
Update comments and documentation
  • Loading branch information
noloader committed Nov 12, 2017
1 parent d28e813 commit bf717f4
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 107 deletions.
2 changes: 1 addition & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ NAMESPACE_END
// 4786: identifier was truncated in debug information
// 4355: 'this' : used in base member initializer list
// 4910: '__declspec(dllexport)' and 'extern' are incompatible on an explicit instantiation
# pragma warning(disable: 4127 4512 4661)
# pragma warning(disable: 4127 4512 4661 4910)
// Security related, possible defects
// http://blogs.msdn.com/b/vcblog/archive/2010/12/14/off-by-default-compiler-warnings-in-visual-c.aspx
# pragma warning(once: 4191 4242 4263 4264 4266 4302 4826 4905 4906 4928)
Expand Down
28 changes: 18 additions & 10 deletions cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ bool CpuId(word32 func, word32 subfunc, word32 output[4])
mov eax, func
mov ecx, subfunc
cpuid
mov edi, output
mov [a], eax
mov [b], ebx
mov [c], ecx
Expand All @@ -130,7 +129,9 @@ bool CpuId(word32 func, word32 subfunc, word32 output[4])
return false;
}

// function 0 returns the highest basic function understood in EAX
// func = 0 returns the highest basic function understood in EAX. If the CPU does
// not return non-0, then it is mostly useless. The code below converts basic
// function value to a true/false return value.
if(func == 0)
return !!output[0];

Expand Down Expand Up @@ -748,9 +749,11 @@ NAMESPACE_END
// *************************** C++ Static Initialization ***************************

ANONYMOUS_NAMESPACE_BEGIN
struct InitializeCpu

class InitCpu
{
InitializeCpu()
public:
InitCpu()
{
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
CryptoPP::DetectX86Features();
Expand All @@ -762,16 +765,21 @@ struct InitializeCpu
}
};

// This is not really needed because HasSSE() and friends can dynamically initialize.
// Everything depends on CPU features so we initialize it once at load time.
// Dynamic initialization will be used if init priorities are not available.

#if HAVE_GCC_INIT_PRIORITY
const InitializeCpu s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 20))) = InitializeCpu();
const InitCpu s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = InitCpu();
#elif HAVE_MSC_INIT_PRIORITY
#pragma warning(disable: 4075)
#pragma init_seg(".CRT$XCU-020")
const InitializeCpu s_init;
#pragma warning(default: 4075)
#pragma warning(disable: 4075)
#pragma init_seg(".CRT$XCU")
const InitCpu s_init;
#pragma warning(default: 4075)
#else
const InitializeCpu& s_init = CryptoPP::Singleton<InitializeCpu>().Ref();
const InitCpu s_init;
#endif

ANONYMOUS_NAMESPACE_END

#endif // CRYPTOPP_IMPORTS
28 changes: 8 additions & 20 deletions cryptlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ CRYPTOPP_COMPILE_ASSERT(sizeof(word64) == 8);
CRYPTOPP_COMPILE_ASSERT(sizeof(dword) == 2*sizeof(word));
#endif

#if 0
class NullNameValuePairs : public NameValuePairs
{
public:
NullNameValuePairs() {} // Clang complains a default ctor must be avilable
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{CRYPTOPP_UNUSED(name); CRYPTOPP_UNUSED(valueType); CRYPTOPP_UNUSED(pValue); return false;}
};
#endif

BufferedTransformation & TheBitBucket()
{
Expand Down Expand Up @@ -945,27 +947,13 @@ int LibraryVersion(CRYPTOPP_NOINLINE_DOTDOTDOT)
}

// ***************** C++ Static Initialization ********************
// We can't put these in the anonymous namespace. DEFAULT_CHANNEL,
// AAD_CHANNEL and g_nullNameValuePairs must be defined in CryptoPP.

#if HAVE_GCC_INIT_PRIORITY
const std::string DEFAULT_CHANNEL __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = "";
const std::string AAD_CHANNEL __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 11))) = "AAD";
const NullNameValuePairs s_nullNameValuePairs __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 12)));
const NameValuePairs &g_nullNameValuePairs = dynamic_cast<const NameValuePairs&>(s_nullNameValuePairs);
#elif HAVE_MSC_INIT_PRIORITY
#pragma warning(disable: 4075)
#pragma init_seg(".CRT$XCU-010")
const std::string DEFAULT_CHANNEL("");

#if 0
const std::string DEFAULT_CHANNEL;
const std::string AAD_CHANNEL("AAD");
const NullNameValuePairs s_nullNameValuePairs;
const NameValuePairs &g_nullNameValuePairs = dynamic_cast<const NameValuePairs&>(s_nullNameValuePairs);
#pragma warning(default: 4075)
#else
const std::string DEFAULT_CHANNEL = "";
const std::string AAD_CHANNEL = "AAD";
const simple_ptr<NullNameValuePairs> s_pNullNameValuePairs(new NullNameValuePairs);
const NameValuePairs &g_nullNameValuePairs = *s_pNullNameValuePairs.m_p;

NullNameValuePairs s_nullNameValuePairs;
const NameValuePairs&g_nullNameValuePairs = dynamic_cast<const NameValuePairs&>(s_nullNameValuePairs);
#endif

NAMESPACE_END // CryptoPP
Expand Down
56 changes: 50 additions & 6 deletions cryptlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ struct CRYPTOPP_DLL DecodingResult
//! then look at the Name namespace documentation to see what the type of each value is, or
//! alternatively, call GetIntValue() with the value name, and if the type is not int, a
//! ValueTypeMismatch exception will be thrown and you can get the actual type from the exception object.
class CRYPTOPP_NO_VTABLE NameValuePairs
//! \sa NullNameValuePairs, g_nullNameValuePairs,
//! <A HREF="http://www.cryptopp.com/wiki/NameValuePairs">NameValuePairs</A> on the Crypto++ wiki
class NameValuePairs
{
public:
virtual ~NameValuePairs() {}
Expand Down Expand Up @@ -445,8 +447,52 @@ class CRYPTOPP_NO_VTABLE NameValuePairs
CRYPTOPP_DLL virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
};

#if CRYPTOPP_DOXYGEN_PROCESSING
//! \class NullNameValuePairs
//! \brief Interface for retrieving values given their names
//! \details This class is used when no names or values are present. Typically a program uses
//! g_nullNameValuePairs rather than creating its own NullNameValuePairs object.
//! \details NullNameValuePairs always existed in cryptlib.cpp. Crypto++ 6.0 moved NullNameValuePairs
//! into the header. This allowed the library to define g_nullNameValuePairs in the header rather
//! than declaring it as extern and placing the definition in the source file. As an external definition
//! the string g_nullNameValuePairs was subject to static initialization order fiasco problems.
//! \sa NameValuePairs, g_nullNameValuePairs,
//! <A HREF="http://www.cryptopp.com/wiki/NameValuePairs">NameValuePairs</A> on the Crypto++ wiki
class NullNameValuePairs : public NameValuePairs
{
public:
NullNameValuePairs() {} // Clang complains a default ctor must be avilable
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{CRYPTOPP_UNUSED(name); CRYPTOPP_UNUSED(valueType); CRYPTOPP_UNUSED(pValue); return false;}
};

// More static initialization order fiasco workarounds. These definitions cannot be extern and
// cannot be static class members because they require a single definition in a source file.
ANONYMOUS_NAMESPACE_BEGIN
const NullNameValuePairs s_nullNameValuePairs;
ANONYMOUS_NAMESPACE_END

//! \brief Default channel for BufferedTransformation
//! \details DEFAULT_CHANNEL is equal to an empty string
//! \details Crypto++ 6.0 placed DEFAULT_CHANNEL in the header, rather than declaring it as extern and
//! placing the definition in the source file. As an external definition the string DEFAULT_CHANNEL
//! was subject to static initialization order fiasco problems.
static const std::string DEFAULT_CHANNEL;

//! \brief Channel for additional authenticated data
//! \details AAD_CHANNEL is equal to "AAD"
//! \details Crypto++ 6.0 placed AAD_CHANNEL in the header, rather than declaring it as extern and
//! placing the definition in the source file. As an external definition the string AAD_CHANNEL
//! was subject to static initialization order fiasco problems.
static const std::string AAD_CHANNEL("AAD");

//! \brief An empty set of name-value pairs
//! \details Crypto++ 6.0 placed g_nullNameValuePairs in the header, rather than declaring it as extern
//! and placing the definition in the source file. As an external definition the g_nullNameValuePairs
//! was subject to static initialization order fiasco problems.
static const NameValuePairs& g_nullNameValuePairs = s_nullNameValuePairs;

// Document additional name spaces which show up elsewhere in the sources.
#if CRYPTOPP_DOXYGEN_PROCESSING
//! \brief Namespace containing value name definitions.
//! \details Name is part of the CryptoPP namespace.
//! \details The semantics of value names, types are:
Expand All @@ -470,7 +516,6 @@ DOCUMENTED_NAMESPACE_END
//! ...
//! CryptoPP::Weak::MD5 md5;
//! </pre>

DOCUMENTED_NAMESPACE_BEGIN(Weak)
// weak and wounded algorithms
DOCUMENTED_NAMESPACE_END
Expand All @@ -483,9 +528,6 @@ DOCUMENTED_NAMESPACE_BEGIN(Test)
// testing and benchmark classes
DOCUMENTED_NAMESPACE_END

//! \brief An empty set of name-value pairs
extern CRYPTOPP_DLL const NameValuePairs &g_nullNameValuePairs;

// ********************************************************

//! \class Clonable
Expand Down Expand Up @@ -1388,13 +1430,15 @@ class CRYPTOPP_NO_VTABLE Waitable
bool Wait(unsigned long milliseconds, CallStack const& callStack);
};

#if 0
//! \brief Default channel for BufferedTransformation
//! \details DEFAULT_CHANNEL is equal to an empty string
extern CRYPTOPP_DLL const std::string DEFAULT_CHANNEL;

//! \brief Channel for additional authenticated data
//! \details AAD_CHANNEL is equal to "AAD"
extern CRYPTOPP_DLL const std::string AAD_CHANNEL;
#endif

//! \brief Interface for buffered transformations
//! \details BufferedTransformation is a generalization of BlockTransformation,
Expand Down
Loading

0 comments on commit bf717f4

Please sign in to comment.