Skip to content

Commit

Permalink
Add remainder of SafeConvert specializations
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Jun 23, 2023
1 parent 37846e0 commit 0cf0845
Showing 1 changed file with 109 additions and 22 deletions.
131 changes: 109 additions & 22 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,35 @@ inline bool SafeConvert(T1 from, T2 &to)
return true;
}

// The following specializations are the product of {word32, sword32, word64, sword64} ->
// {word32, sword32, word64, sword64}. There are 16 of them, but we can omit specializations
// of {word64} -> {word64}, {word32} -> {word32}, etc.
//
// The list below proceeds to list the conversion to word64 (3 each), followed by
// sword64 (3 each), followed by word32 (3 each), and finally follwed by sword32 (3 each).

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(sword64 from, word64 &to)
{
if (from < 0)
return false;
to = static_cast<word64>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word32 from, word64 &to)
Expand All @@ -738,24 +761,24 @@ inline bool SafeConvert(word32 from, word64 &to)
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word64 from, word32 &to)
inline bool SafeConvert(sword32 from, word64 &to)
{
if (from > static_cast<word64>(std::numeric_limits<word32>::max()))
if (from < 0)
return false;
to = static_cast<word32>(from);
to = static_cast<word64>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word64 from, sword64 &to)
Expand All @@ -770,24 +793,86 @@ inline bool SafeConvert(word64 from, sword64 &to)
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(sword64 from, word64 &to)
inline bool SafeConvert(word32 from, sword64 &to)
{
to = static_cast<sword64>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(sword32 from, sword64 &to)
{
to = static_cast<sword64>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word64 from, word32 &to)
{
if (from > static_cast<word64>(std::numeric_limits<word32>::max()))
return false;
to = static_cast<word32>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(sword64 from, word32 &to)
{
if (from < 0)
return false;
to = static_cast<word64>(from);
else if (from > static_cast<sword64>(std::numeric_limits<word32>::max()))
return false;
to = static_cast<word32>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(sword32 from, word32 &to)
{
if (from < 0)
return false;
to = static_cast<word32>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word64 from, sword32 &to)
Expand All @@ -802,24 +887,26 @@ inline bool SafeConvert(word64 from, sword32 &to)
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(sword32 from, word64 &to)
inline bool SafeConvert(sword64 from, sword32 &to)
{
if (from < 0)
if (from > static_cast<sword64>(std::numeric_limits<sword32>::max()))
return false;
to = static_cast<word64>(from);
else if (from < static_cast<sword64>(std::numeric_limits<sword32>::min()))
return false;
to = static_cast<sword32>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert from \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \details if the function returns true, then it is safe to use \p to. If the function
/// returns false, then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word32 from, sword32 &to)
Expand Down

0 comments on commit 0cf0845

Please sign in to comment.