summaryrefslogtreecommitdiff
path: root/integer.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2017-03-25 16:38:42 -0400
committerJeffrey Walton <noloader@gmail.com>2017-03-25 16:38:42 -0400
commit0e55f5ac7d98f3c852ace7b4622f40cfa60b629f (patch)
tree350439fd5f1d5a2b43b4be0e55fe9a572b533a8d /integer.cpp
parent6c50a99254bcb76fb9b2b476b392bbfe2259f409 (diff)
downloadcryptopp-git-0e55f5ac7d98f3c852ace7b4622f40cfa60b629f.tar.gz
Remove g_pAssignIntToInteger pointer, add CRYPTOPP_NO_ASSIGN_TO_INTEGER (Issue 389)
This effectively decouples Integer and Public Key from the rest of the library. The change means a compile time define is used rather than a runtime pointer. It avoids the race with Issue 389. The Public Key algorithms will fail if you use them. For example, running the self tests with CRYPTOPP_NO_ASSIGN_TO_INTEGER in effect results in "CryptoPP::Exception caught: NameValuePairs: type mismatch for 'EquivalentTo', stored 'i', trying to retrieve 'N8CryptoPP7IntegerE'". The exception is expected, and the same happend when g_pAssignIntToInteger was present.
Diffstat (limited to 'integer.cpp')
-rw-r--r--integer.cpp60
1 files changed, 32 insertions, 28 deletions
diff --git a/integer.cpp b/integer.cpp
index 700e8902..8df0b567 100644
--- a/integer.cpp
+++ b/integer.cpp
@@ -72,6 +72,12 @@
// ***************** C++ Static Initialization ********************
NAMESPACE_BEGIN(CryptoPP)
+static void SetFunctionPointers();
+InitializeInteger::InitializeInteger()
+{
+ SetFunctionPointers();
+}
+
template <long i>
struct NewInteger
{
@@ -80,54 +86,28 @@ struct NewInteger
return new Integer(i);
}
};
-
-static void SetFunctionPointers();
-bool AssignIntToInteger(const std::type_info &valueType, void *pInteger, const void *pInt);
NAMESPACE_END
ANONYMOUS_NAMESPACE_BEGIN
-struct InitializeInteger
-{
- InitializeInteger()
- {
- CryptoPP::SetFunctionPointers();
- CryptoPP::g_pAssignIntToInteger = (CryptoPP::PAssignIntToInteger)CryptoPP::AssignIntToInteger;
- }
-};
-
#if HAVE_GCC_INIT_PRIORITY
-const InitializeInteger s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 30))) = InitializeInteger();
+const CryptoPP::InitializeInteger s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 30))) = CryptoPP::InitializeInteger();
const CryptoPP::Integer s_zero __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 31))) = CryptoPP::Integer(0L);
const CryptoPP::Integer s_one __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 32))) = CryptoPP::Integer(1L);
const CryptoPP::Integer s_two __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 33))) = CryptoPP::Integer(2L);
#elif HAVE_MSC_INIT_PRIORITY
#pragma warning(disable: 4075)
#pragma init_seg(".CRT$XCU-030")
-const InitializeInteger s_init;
+const CryptoPP::InitializeInteger s_init;
const CryptoPP::Integer s_zero(0L);
const CryptoPP::Integer s_one(1L);
const CryptoPP::Integer s_two(2L);
#pragma warning(default: 4075)
-#else
-const InitializeInteger& s_init = CryptoPP::Singleton<InitializeInteger>().Ref();
-const CryptoPP::Integer& s_zero = CryptoPP::Singleton<CryptoPP::Integer, CryptoPP::NewInteger<0L> >().Ref();
-const CryptoPP::Integer& s_one = CryptoPP::Singleton<CryptoPP::Integer, CryptoPP::NewInteger<1L> >().Ref();
-const CryptoPP::Integer& s_two = CryptoPP::Singleton<CryptoPP::Integer, CryptoPP::NewInteger<2L> >().Ref();
#endif
ANONYMOUS_NAMESPACE_END
// ***************** Library code ********************
NAMESPACE_BEGIN(CryptoPP)
-
-bool AssignIntToInteger(const std::type_info &valueType, void *pInteger, const void *pInt)
-{
- if (valueType != typeid(Integer))
- return false;
- *reinterpret_cast<Integer *>(pInteger) = *reinterpret_cast<const int *>(pInt);
- return true;
-}
-
inline static int Compare(const word *A, const word *B, size_t N)
{
while (N--)
@@ -3045,17 +3025,29 @@ Integer Integer::Power2(size_t e)
const Integer &Integer::Zero()
{
+#if HAVE_GCC_INIT_PRIORITY || HAVE_MSC_INIT_PRIORITY
return s_zero;
+#else
+ return Singleton<Integer, NewInteger<0L> >().Ref();
+#endif
}
const Integer &Integer::One()
{
+#if HAVE_GCC_INIT_PRIORITY || HAVE_MSC_INIT_PRIORITY
return s_one;
+#else
+ return Singleton<Integer, NewInteger<1L> >().Ref();
+#endif
}
const Integer &Integer::Two()
{
+#if HAVE_GCC_INIT_PRIORITY || HAVE_MSC_INIT_PRIORITY
return s_two;
+#else
+ return Singleton<Integer, NewInteger<2L> >().Ref();
+#endif
}
bool Integer::operator!() const
@@ -4749,6 +4741,18 @@ std::string IntToString<word64>(word64 value, unsigned int base)
return result;
}
+#ifndef CRYPTOPP_NO_ASSIGN_TO_INTEGER
+// Allow the linker to discard Integer code if not needed.
+// Also see http://github.com/weidai11/cryptopp/issues/389.
+bool AssignIntToInteger(const std::type_info &valueType, void *pInteger, const void *pInt)
+{
+ if (valueType != typeid(Integer))
+ return false;
+ *reinterpret_cast<Integer *>(pInteger) = *reinterpret_cast<const int *>(pInt);
+ return true;
+}
+#endif
+
NAMESPACE_END
#endif