summaryrefslogtreecommitdiff
path: root/misc.h
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2019-10-03 04:12:16 -0400
committerJeffrey Walton <noloader@gmail.com>2019-10-03 04:12:16 -0400
commit366fdce86f968500994d9fb81bd0d37053a01126 (patch)
tree38ab77659e3142dabad73e860b339faf1a004dcc /misc.h
parentf5c817221bace9baec5ace88b3c7588d21a34c0e (diff)
downloadcryptopp-git-366fdce86f968500994d9fb81bd0d37053a01126.tar.gz
Add BytePtr and ConstBytePtr overloads for SecByteBlock (GH #890)
Diffstat (limited to 'misc.h')
-rw-r--r--misc.h27
1 files changed, 25 insertions, 2 deletions
diff --git a/misc.h b/misc.h
index fa10ff90..97b8de1b 100644
--- a/misc.h
+++ b/misc.h
@@ -7,6 +7,7 @@
#define CRYPTOPP_MISC_H
#include "cryptlib.h"
+#include "secblockfwd.h"
#include "smartptr.h"
#include "stdcpp.h"
#include "trap.h"
@@ -432,6 +433,7 @@ inline size_t PtrByteDiff(const PTR pointer1, const PTR pointer2)
/// \param str std::string
/// \details BytePtr returns NULL pointer for an empty string.
/// \return Pointer to the first element of a string
+/// \since Crypto++ 8.0
inline byte* BytePtr(std::string& str)
{
// Caller wants a writeable pointer
@@ -442,16 +444,32 @@ inline byte* BytePtr(std::string& str)
return reinterpret_cast<byte*>(&str[0]);
}
+/// \brief Pointer to the first element of a string
+/// \param str SecByteBlock
+/// \details BytePtr returns NULL pointer for an empty string.
+/// \return Pointer to the first element of a string
+/// \since Crypto++ 8.3
+byte* BytePtr(SecByteBlock& str);
+
/// \brief Const pointer to the first element of a string
/// \param str std::string
/// \details ConstBytePtr returns non-NULL pointer for an empty string.
/// \return Pointer to the first element of a string
+/// \since Crypto++ 8.0
inline const byte* ConstBytePtr(const std::string& str)
{
- // Use c_str() so a pointer is always available
- return reinterpret_cast<const byte*>(str.c_str());
+ if (str.empty())
+ return NULLPTR;
+ return reinterpret_cast<const byte*>(&str[0]);
}
+/// \brief Const pointer to the first element of a string
+/// \param str SecByteBlock
+/// \details ConstBytePtr returns non-NULL pointer for an empty string.
+/// \return Pointer to the first element of a string
+/// \since Crypto++ 8.3
+const byte* ConstBytePtr(const SecByteBlock& str);
+
/// \brief Size of a string
/// \param str std::string
/// \return size of a string
@@ -460,6 +478,11 @@ inline size_t BytePtrSize(const std::string& str)
return str.size();
}
+/// \brief Size of a string
+/// \param str SecByteBlock
+/// \return size of a string
+size_t BytePtrSize(const SecByteBlock& str);
+
#if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
/// \brief Bounds checking replacement for memcpy()