Skip to content

Commit

Permalink
Add C++ nullptr support (Issue 383)
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Mar 1, 2017
1 parent 5fb2f5d commit 5efb019
Show file tree
Hide file tree
Showing 92 changed files with 484 additions and 463 deletions.
2 changes: 1 addition & 1 deletion algparam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

NAMESPACE_BEGIN(CryptoPP)

PAssignIntToInteger g_pAssignIntToInteger = NULL;
PAssignIntToInteger g_pAssignIntToInteger = NULLPTR;

bool CombinedNameValuePairs::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
Expand Down
16 changes: 8 additions & 8 deletions algparam.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class ConstByteArrayParameter
//! \param deepCopy flag indicating whether the data should be copied
//! \details The deepCopy option is used when the NameValuePairs object can't
//! keep a copy of the data available
ConstByteArrayParameter(const char *data = NULL, bool deepCopy = false)
: m_deepCopy(false), m_data(NULL), m_size(0)
ConstByteArrayParameter(const char *data = NULLPTR, bool deepCopy = false)
: m_deepCopy(false), m_data(NULLPTR), m_size(0)
{
Assign((const byte *)data, data ? strlen(data) : 0, deepCopy);
}
Expand All @@ -47,7 +47,7 @@ class ConstByteArrayParameter
//! \details The deepCopy option is used when the NameValuePairs object can't
//! keep a copy of the data available
ConstByteArrayParameter(const byte *data, size_t size, bool deepCopy = false)
: m_deepCopy(false), m_data(NULL), m_size(0)
: m_deepCopy(false), m_data(NULLPTR), m_size(0)
{
Assign(data, size, deepCopy);
}
Expand All @@ -59,7 +59,7 @@ class ConstByteArrayParameter
//! \details The deepCopy option is used when the NameValuePairs object can't
//! keep a copy of the data available
template <class T> ConstByteArrayParameter(const T &string, bool deepCopy = false)
: m_deepCopy(false), m_data(NULL), m_size(0)
: m_deepCopy(false), m_data(NULLPTR), m_size(0)
{
CRYPTOPP_COMPILE_ASSERT(sizeof(typename T::value_type) == 1);
Assign((const byte *)string.data(), string.size(), deepCopy);
Expand Down Expand Up @@ -107,7 +107,7 @@ class ByteArrayParameter
//! \brief Construct a ByteArrayParameter
//! \param data a memory buffer
//! \param size the length of the memory buffer
ByteArrayParameter(byte *data = NULL, unsigned int size = 0)
ByteArrayParameter(byte *data = NULLPTR, unsigned int size = 0)
: m_data(data), m_size(size) {}

//! \brief Construct a ByteArrayParameter
Expand Down Expand Up @@ -221,13 +221,13 @@ class GetValueHelperClass
};

template <class BASE, class T>
GetValueHelperClass<T, BASE> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
GetValueHelperClass<T, BASE> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULLPTR)
{
return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue, searchFirst);
}

template <class T>
GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULLPTR)
{
return GetValueHelperClass<T, T>(pObject, name, valueType, pValue, searchFirst);
}
Expand Down Expand Up @@ -387,7 +387,7 @@ class AlgorithmParametersTemplate : public AlgorithmParametersBase
void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
{
// special case for retrieving an Integer parameter when an int was passed in
if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
if (!(g_pAssignIntToInteger != NULLPTR && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
{
NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType);
*reinterpret_cast<T *>(pValue) = m_value;
Expand Down
4 changes: 2 additions & 2 deletions asn.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class EncodedObjectFilter : public Filter
//! \param attachment a BufferedTrasformation to attach to this object
//! \param nObjects
//! \param flags bitwise OR of EncodedObjectFilter::Flag
EncodedObjectFilter(BufferedTransformation *attachment = NULL, unsigned int nObjects = 1, word32 flags = 0);
EncodedObjectFilter(BufferedTransformation *attachment = NULLPTR, unsigned int nObjects = 1, word32 flags = 0);

//! \brief Input a byte buffer for processing
//! \param inString the byte buffer to process
Expand Down Expand Up @@ -361,7 +361,7 @@ class ASNOptional : public member_ptr<T>
//! \param out BufferedTransformation object
void DEREncode(BufferedTransformation &out)
{
if (this->get() != NULL)
if (this->get() != NULLPTR)
this->get()->DEREncode(out);
}
};
Expand Down
4 changes: 2 additions & 2 deletions base32.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Base32Encoder : public SimpleProxyFilter
//! \details Base32Encoder() constructs a default encoder. The constructor lacks fields for padding and
//! line breaks. You must use IsolatedInitialize() to change the default padding character or suppress it.
//! \sa IsolatedInitialize() for an example of modifying a Base32Encoder after construction.
Base32Encoder(BufferedTransformation *attachment = NULL, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
Base32Encoder(BufferedTransformation *attachment = NULLPTR, bool uppercase = true, int groupSize = 0, const std::string &separator = ":", const std::string &terminator = "")
: SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
{
IsolatedInitialize(MakeParameters(Name::Uppercase(), uppercase)(Name::GroupSize(), groupSize)(Name::Separator(), ConstByteArrayParameter(separator))(Name::Terminator(), ConstByteArrayParameter(terminator)));
Expand Down Expand Up @@ -64,7 +64,7 @@ class Base32Decoder : public BaseN_Decoder
//! \brief Construct a Base32Decoder
//! \param attachment a BufferedTrasformation to attach to this object
//! \sa IsolatedInitialize() for an example of modifying a Base32Decoder after construction.
Base32Decoder(BufferedTransformation *attachment = NULL)
Base32Decoder(BufferedTransformation *attachment = NULLPTR)
: BaseN_Decoder(GetDefaultDecodingLookupArray(), 5, attachment) {}

//! \brief Initialize or reinitialize this object, without signal propagation
Expand Down
8 changes: 4 additions & 4 deletions base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Base64Encoder : public SimpleProxyFilter
//! \details Base64Encoder constructs a default encoder. The constructor lacks a parameter for padding, and you must
//! use IsolatedInitialize() to modify the Base64Encoder after construction.
//! \sa IsolatedInitialize() for an example of modifying an encoder after construction.
Base64Encoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = true, int maxLineLength = 72)
Base64Encoder(BufferedTransformation *attachment = NULLPTR, bool insertLineBreaks = true, int maxLineLength = 72)
: SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
{
IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength));
Expand Down Expand Up @@ -62,7 +62,7 @@ class Base64Decoder : public BaseN_Decoder
//! \brief Construct a Base64Decoder
//! \param attachment a BufferedTrasformation to attach to this object
//! \sa IsolatedInitialize() for an example of modifying an encoder after construction.
Base64Decoder(BufferedTransformation *attachment = NULL)
Base64Decoder(BufferedTransformation *attachment = NULLPTR)
: BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}

//! \brief Initialize or reinitialize this object, without signal propagation
Expand Down Expand Up @@ -109,7 +109,7 @@ class Base64URLEncoder : public SimpleProxyFilter
//! after constructing a Base64URLEncoder.
//! \sa Base64Encoder for an encoder that provides a classic alphabet, and Base64URLEncoder::IsolatedInitialize
//! for an example of modifying an encoder after construction.
Base64URLEncoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = false, int maxLineLength = -1)
Base64URLEncoder(BufferedTransformation *attachment = NULLPTR, bool insertLineBreaks = false, int maxLineLength = -1)
: SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
{
CRYPTOPP_UNUSED(insertLineBreaks), CRYPTOPP_UNUSED(maxLineLength);
Expand Down Expand Up @@ -139,7 +139,7 @@ class Base64URLDecoder : public BaseN_Decoder
//! \param attachment a BufferedTrasformation to attach to this object
//! \details Base64URLDecoder() constructs a default decoder using a web safe alphabet.
//! \sa Base64Decoder for a decoder that provides a classic alphabet.
Base64URLDecoder(BufferedTransformation *attachment = NULL)
Base64URLDecoder(BufferedTransformation *attachment = NULLPTR)
: BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}

//! \brief Initialize or reinitialize this object, without signal propagation
Expand Down
20 changes: 10 additions & 10 deletions basecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
public:
//! \brief Construct a BaseN_Encoder
//! \param attachment a BufferedTransformation to attach to this object
BaseN_Encoder(BufferedTransformation *attachment=NULL)
: m_alphabet(NULL), m_padding(0), m_bitsPerChar(0)
BaseN_Encoder(BufferedTransformation *attachment=NULLPTR)
: m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
, m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
{Detach(attachment);}

Expand All @@ -32,8 +32,8 @@ class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
//! \param padding the character to use as padding
//! \pre log2base must be between 1 and 7 inclusive
//! \throws InvalidArgument if log2base is not between 1 and 7
BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULL, int padding=-1)
: m_alphabet(NULL), m_padding(0), m_bitsPerChar(0)
BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULLPTR, int padding=-1)
: m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
, m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
{
Detach(attachment);
Expand Down Expand Up @@ -62,8 +62,8 @@ class CRYPTOPP_DLL BaseN_Decoder : public Unflushable<Filter>
//! \param attachment a BufferedTransformation to attach to this object
//! \details padding is set to -1, which means use default padding. If not
//! required, then the value must be set via IsolatedInitialize().
BaseN_Decoder(BufferedTransformation *attachment=NULL)
: m_lookup(0), m_padding(0), m_bitsPerChar(0)
BaseN_Decoder(BufferedTransformation *attachment=NULLPTR)
: m_lookup(NULLPTR), m_padding(0), m_bitsPerChar(0)
, m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
{Detach(attachment);}

Expand All @@ -75,8 +75,8 @@ class CRYPTOPP_DLL BaseN_Decoder : public Unflushable<Filter>
//! the number of elements (like 32).
//! \details padding is set to -1, which means use default padding. If not
//! required, then the value must be set via IsolatedInitialize().
BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULL)
: m_lookup(0), m_padding(0), m_bitsPerChar(0)
BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULLPTR)
: m_lookup(NULLPTR), m_padding(0), m_bitsPerChar(0)
, m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
{
Detach(attachment);
Expand Down Expand Up @@ -112,15 +112,15 @@ class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
public:
//! \brief Construct a Grouper
//! \param attachment a BufferedTransformation to attach to this object
Grouper(BufferedTransformation *attachment=NULL)
Grouper(BufferedTransformation *attachment=NULLPTR)
: m_groupSize(0), m_counter(0) {Detach(attachment);}

//! \brief Construct a Grouper
//! \param groupSize the size of the grouping
//! \param separator the separator to use between groups
//! \param terminator the terminator appeand after processing
//! \param attachment a BufferedTransformation to attach to this object
Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULL)
Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULLPTR)
: m_groupSize(0), m_counter(0)
{
Detach(attachment);
Expand Down
12 changes: 6 additions & 6 deletions bench1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
{
blocks *= 2;
for (; i<blocks; i++)
cipher.ProcessAndXorMultipleBlocks(buf, NULL, buf, nBlocks);
cipher.ProcessAndXorMultipleBlocks(buf, NULLPTR, buf, nBlocks);
timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
}
while (timeTaken < 2.0/3*timeTotal);
Expand Down Expand Up @@ -192,7 +192,7 @@ void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValue
}

template <class T_FactoryOutput, class T_Interface>
void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs)
void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
{
CRYPTOPP_UNUSED(params);
std::string name(factoryName ? factoryName : "");
Expand All @@ -212,14 +212,14 @@ void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char
}

template <class T_FactoryOutput>
void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs)
void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
{
CRYPTOPP_UNUSED(params);
BenchMarkByName2<T_FactoryOutput, T_FactoryOutput>(factoryName, keyLength, displayName, params);
}

template <class T>
void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs)
void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
{
CRYPTOPP_UNUSED(params);
std::string name = factoryName;
Expand Down Expand Up @@ -373,15 +373,15 @@ void BenchmarkAll(double t, double hertz)
char timeBuf[64];
errno_t err;

const time_t endTime = time(NULL);
const time_t endTime = time(NULLPTR);
err = localtime_s(&localTime, &endTime);
CRYPTOPP_ASSERT(err == 0);
err = asctime_s(timeBuf, sizeof(timeBuf), &localTime);
CRYPTOPP_ASSERT(err == 0);

std::cout << "\nTest ended at " << timeBuf;
#else
const time_t endTime = time(NULL);
const time_t endTime = time(NULLPTR);
std::cout << "\nTest ended at " << asctime(localtime(&endTime));
#endif
#endif
Expand Down
6 changes: 3 additions & 3 deletions blake2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ void BLAKE2_Base<W, T_64bit>::UncheckedSetKey(const byte *key, unsigned int leng
template <class W, bool T_64bit>
BLAKE2_Base<W, T_64bit>::BLAKE2_Base() : m_state(1), m_block(1), m_digestSize(DIGESTSIZE), m_treeMode(false)
{
UncheckedSetKey(NULL, 0, g_nullNameValuePairs);
UncheckedSetKey(NULLPTR, 0, g_nullNameValuePairs);
Restart();
}

Expand All @@ -351,7 +351,7 @@ BLAKE2_Base<W, T_64bit>::BLAKE2_Base(bool treeMode, unsigned int digestSize) : m
{
CRYPTOPP_ASSERT(digestSize <= DIGESTSIZE);

UncheckedSetKey(NULL, 0, g_nullNameValuePairs);
UncheckedSetKey(NULLPTR, 0, g_nullNameValuePairs);
Restart();
}

Expand Down Expand Up @@ -392,7 +392,7 @@ void BLAKE2_Base<W, T_64bit>::Restart(const BLAKE2_ParameterBlock<T_64bit>& bloc
State& state = *m_state.data();
state.t[0] = state.t[1] = 0, state.f[0] = state.f[1] = 0, state.length = 0;

if (counter != NULL)
if (counter != NULLPTR)
{
state.t[0] = counter[0];
state.t[1] = counter[1];
Expand Down
8 changes: 4 additions & 4 deletions blake2.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ class BLAKE2b : public BLAKE2_Base<word64, true>
//! \param personalizationLength the size of the byte array
//! \param treeMode flag indicating tree mode
//! \param digestSize the digest size, in bytes
BLAKE2b(const byte *key, size_t keyLength, const byte* salt = NULL, size_t saltLength = 0,
const byte* personalization = NULL, size_t personalizationLength = 0,
BLAKE2b(const byte *key, size_t keyLength, const byte* salt = NULLPTR, size_t saltLength = 0,
const byte* personalization = NULLPTR, size_t personalizationLength = 0,
bool treeMode=false, unsigned int digestSize = DIGESTSIZE)
: ThisBase(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
};
Expand Down Expand Up @@ -302,8 +302,8 @@ class BLAKE2s : public BLAKE2_Base<word32, false>
//! \param personalizationLength the size of the byte array
//! \param treeMode flag indicating tree mode
//! \param digestSize the digest size, in bytes
BLAKE2s(const byte *key, size_t keyLength, const byte* salt = NULL, size_t saltLength = 0,
const byte* personalization = NULL, size_t personalizationLength = 0,
BLAKE2s(const byte *key, size_t keyLength, const byte* salt = NULLPTR, size_t saltLength = 0,
const byte* personalization = NULLPTR, size_t personalizationLength = 0,
bool treeMode=false, unsigned int digestSize = DIGESTSIZE)
: ThisBase(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
};
Expand Down
4 changes: 2 additions & 2 deletions channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ byte * ChannelSwitch::ChannelCreatePutSpace(const std::string &channel, size_t &
return target.ChannelCreatePutSpace(ch, size);
}
size = 0;
return NULL;
return NULLPTR;
}

size_t ChannelSwitch::ChannelPutModifiable2(const std::string &channel, byte *inString, size_t length, int messageEnd, bool blocking)
Expand All @@ -261,7 +261,7 @@ size_t ChannelSwitch::ChannelPutModifiable2(const std::string &channel, byte *in

void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination)
{
m_defaultRoutes.push_back(DefaultRoute(&destination, value_ptr<std::string>(NULL)));
m_defaultRoutes.push_back(DefaultRoute(&destination, value_ptr<std::string>(NULLPTR)));
}

void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination)
Expand Down
23 changes: 23 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,22 @@ NAMESPACE_END
# define CRYPTOPP_CXX11_CONSTEXPR 1
#endif // constexpr compilers

// nullptr_t: MS at VS2010 (16.00); GCC at 4.6; Clang at 3.3; Intel 12.0; SunCC 12.4.
// Intel has upported the feature since at least ICPC 12.00
#if (CRYPTOPP_MSC_VERSION >= 1600)
# define CRYPTOPP_CXX11_NULLPTR 1
#elif (__INTEL_COMPILER >= 1200)
# define CRYPTOPP_CXX11_NULLPTR 1
#elif defined(__clang__)
# if __has_feature(cxx_nullptr)
# define CRYPTOPP_CXX11_NULLPTR 1
# endif
#elif (CRYPTOPP_GCC_VERSION >= 40600)
# define CRYPTOPP_CXX11_NULLPTR 1
#elif (__SUNPRO_CC >= 0x5130)
# define CRYPTOPP_CXX11_NULLPTR 1
#endif // nullptr_t compilers

// TODO: Emplacement, R-values and Move semantics
// Needed because we are catching warnings with GCC and MSC

Expand Down Expand Up @@ -920,6 +936,13 @@ NAMESPACE_END
# define CRYPTOPP_CONSTANT(x) constexpr static int x;
#endif

// Hack... C++11 nullptr_t type safety and analysis
#if defined(CRYPTOPP_CXX11_NULLPTR) && !defined(NULLPTR)
# define NULLPTR nullptr
#elif !defined(NULLPTR)
# define NULLPTR NULL
#endif // CRYPTOPP_CXX11_NULLPTR

// OK to comment the following out, but please report it so we can fix it.
// C++17 value taken from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4567.pdf.
#if (defined(__cplusplus) && (__cplusplus >= 199711L) && (__cplusplus < 201402L)) && !defined(CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE)
Expand Down
Loading

0 comments on commit 5efb019

Please sign in to comment.