From 39573a06d30dbd3d2a691f101d2daf7379d071b1 Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Sat, 9 Mar 2019 14:57:04 +0100 Subject: [PATCH] include/types: beef sha_digest_t up with encode and compare. BEWARE: sha_digest_t is part of our public. The commit tries to preserve it as the cost of avoiding a clean-up. Signed-off-by: Radoslaw Zarzynski --- src/include/types.h | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/include/types.h b/src/include/types.h index 92a30f56248..04c62e38974 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -561,6 +561,8 @@ WRITE_CMP_OPERATORS_1(errorcode32_t, code) template struct sha_digest_t { constexpr static uint32_t SIZE = S; + // TODO: we might consider std::array in the future. Avoiding it for now + // as sha_digest_t is a part of our public API. unsigned char v[S] = {0}; string to_str() const { @@ -571,8 +573,29 @@ struct sha_digest_t { } return string(str); } - sha_digest_t(const unsigned char *_v) { memcpy(v, _v, S); }; + sha_digest_t(const unsigned char *_v) { memcpy(v, _v, SIZE); }; sha_digest_t() {} + + bool operator==(const sha_digest_t& r) const { + return ::memcmp(v, r.v, SIZE) == 0; + } + bool operator!=(const sha_digest_t& r) const { + return ::memcmp(v, r.v, SIZE) != 0; + } + + void encode(bufferlist &bl) const { + // copy to avoid reinterpret_cast, is_pod and other nasty things + using ceph::encode; + std::array tmparr; + memcpy(tmparr.data(), v, SIZE); + encode(tmparr, bl); + } + void decode(bufferlist::const_iterator &bl) { + using ceph::decode; + std::array tmparr; + decode(tmparr, bl); + memcpy(v, tmparr.data(), SIZE); + } }; template @@ -582,6 +605,10 @@ inline ostream &operator<<(ostream &out, const sha_digest_t &b) { } using sha1_digest_t = sha_digest_t<20>; +WRITE_CLASS_ENCODER(sha1_digest_t) + using sha256_digest_t = sha_digest_t<32>; +WRITE_CLASS_ENCODER(sha256_digest_t) + #endif -- 2.47.3