From: Radoslaw Zarzynski Date: Sat, 9 Mar 2019 13:57:04 +0000 (+0100) Subject: include/types: beef sha_digest_t up with encode and compare. X-Git-Tag: v14.2.0~23^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=39573a06d30dbd3d2a691f101d2daf7379d071b1;p=ceph.git 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 --- diff --git a/src/include/types.h b/src/include/types.h index 92a30f562486..04c62e389740 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