This is an automated email from the ASF dual-hosted git repository.
amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new dac7b2551 CryptoContext: Clean up to avoid compiler problem. (#9521)
dac7b2551 is described below
commit dac7b2551c7c4d406059963dc5521663367456bf
Author: Alan M. Carroll <[email protected]>
AuthorDate: Wed Mar 15 11:09:07 2023 -0500
CryptoContext: Clean up to avoid compiler problem. (#9521)
---
include/tscore/CryptoHash.h | 86 ++++++++++++++++++++++++---------------------
include/tscore/MD5.h | 4 +--
include/tscore/MMH.h | 2 +-
include/tscore/SHA256.h | 2 +-
4 files changed, 50 insertions(+), 44 deletions(-)
diff --git a/include/tscore/CryptoHash.h b/include/tscore/CryptoHash.h
index 02c1f9d71..c486bacc4 100644
--- a/include/tscore/CryptoHash.h
+++ b/include/tscore/CryptoHash.h
@@ -111,53 +111,42 @@ union CryptoHash {
extern CryptoHash const CRYPTO_HASH_ZERO;
-/** Protocol class for a crypto hash context.
-
- A hash of this type is used for strong hashing, such as for URLs.
-*/
-class CryptoContextBase
+class CryptoContext
{
- typedef CryptoContextBase self; ///< Self reference type.
public:
- /// Destructor (force virtual)
- virtual ~CryptoContextBase() {}
- /// Update the hash with @a data of @a length bytes.
- virtual bool update(void const *data, int length) = 0;
- /// Finalize and extract the @a hash.
- virtual bool finalize(CryptoHash &hash) = 0;
+ /** Protocol class for a crypto hash context.
- /// Convenience overload.
- bool finalize(CryptoHash *hash);
+ A hash of this type is used for strong hashing, such as for URLs.
+ */
+ class Hasher
+ {
+ using self_type = Hasher; ///< Self reference type.
+ public:
+ /// Destructor (force virtual)
+ virtual ~Hasher() {}
+ /// Update the hash with @a data of @a length bytes.
+ virtual bool update(void const *data, int length) = 0;
+ /// Finalize and extract the @a hash.
+ virtual bool finalize(CryptoHash &hash) = 0;
+
+ /// Convenience overload.
+ bool finalize(CryptoHash *hash);
+
+ protected:
+ EVP_MD_CTX *_ctx = nullptr;
+ };
+
+ CryptoContext();
+ /// Update the hash with @a data of @a length bytes.
+ bool update(void const *data, int length);
/// Convenience - compute final @a hash for @a data.
/// @note This is just as fast as the previous style, as a new context must
be initialized
/// every time this is done.
bool hash_immediate(CryptoHash &hash, void const *data, int length);
-protected:
- EVP_MD_CTX *_ctx = nullptr;
-};
-
-inline bool
-CryptoContextBase::hash_immediate(CryptoHash &hash, void const *data, int
length)
-{
- return this->update(data, length) && this->finalize(hash);
-}
-
-inline bool
-CryptoContextBase::finalize(CryptoHash *hash)
-{
- return this->finalize(*hash);
-}
-
-class CryptoContext : public CryptoContextBase
-{
-public:
- CryptoContext();
- /// Update the hash with @a data of @a length bytes.
- bool update(void const *data, int length) override;
/// Finalize and extract the @a hash.
- bool finalize(CryptoHash &hash) override;
+ bool finalize(CryptoHash &hash);
enum HashType {
UNSPECIFIED,
@@ -168,23 +157,40 @@ public:
}; ///< What type of hash we really are.
static HashType Setting;
- ~CryptoContext() { reinterpret_cast<CryptoContextBase
*>(_base)->~CryptoContextBase(); }
+ ~CryptoContext();
private:
static size_t constexpr OBJ_SIZE = 256;
char _base[OBJ_SIZE];
};
+inline bool
+CryptoContext::Hasher::finalize(CryptoHash *hash)
+{
+ return this->finalize(*hash);
+}
+
inline bool
CryptoContext::update(void const *data, int length)
{
- return reinterpret_cast<CryptoContextBase *>(_base)->update(data, length);
+ return reinterpret_cast<Hasher *>(_base)->update(data, length);
}
inline bool
CryptoContext::finalize(CryptoHash &hash)
{
- return reinterpret_cast<CryptoContextBase *>(_base)->finalize(hash);
+ return reinterpret_cast<Hasher *>(_base)->finalize(hash);
+}
+
+inline bool
+CryptoContext::hash_immediate(CryptoHash &hash, void const *data, int length)
+{
+ return this->update(data, length) && this->finalize(hash);
+}
+
+inline CryptoContext::~CryptoContext()
+{
+ std::destroy_at(reinterpret_cast<Hasher *>(_base));
}
ts::BufferWriter &bwformat(ts::BufferWriter &w, ts::BWFSpec const &spec,
ats::CryptoHash const &hash);
diff --git a/include/tscore/MD5.h b/include/tscore/MD5.h
index b26a2e1ca..67171f7dd 100644
--- a/include/tscore/MD5.h
+++ b/include/tscore/MD5.h
@@ -31,7 +31,7 @@
#include <openssl/evp.h>
#endif
-class MD5Context : public ats::CryptoContextBase
+class MD5Context : public ats::CryptoContext::Hasher
{
public:
MD5Context()
@@ -90,4 +90,4 @@ private:
#endif
};
-typedef CryptoHash INK_MD5;
+using INK_MD5 = CryptoHash;
diff --git a/include/tscore/MMH.h b/include/tscore/MMH.h
index 15d74b642..efa52094b 100644
--- a/include/tscore/MMH.h
+++ b/include/tscore/MMH.h
@@ -50,7 +50,7 @@ int ink_code_MMH(unsigned char *input, int len, unsigned char
*sixteen_byte_hash
cost.
*/
-class MMHContext : public ats::CryptoContextBase
+class MMHContext : public ats::CryptoContext::Hasher
{
protected:
MMH_CTX _ctx;
diff --git a/include/tscore/SHA256.h b/include/tscore/SHA256.h
index 268908d9b..a22345d9a 100644
--- a/include/tscore/SHA256.h
+++ b/include/tscore/SHA256.h
@@ -31,7 +31,7 @@
#include <openssl/evp.h>
#endif
-class SHA256Context : public ats::CryptoContextBase
+class SHA256Context : public ats::CryptoContext::Hasher
{
public:
SHA256Context()