]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
include/types: beef sha_digest_t up with encode and compare.
authorRadoslaw Zarzynski <rzarzyns@redhat.com>
Sat, 9 Mar 2019 13:57:04 +0000 (14:57 +0100)
committerRadoslaw Zarzynski <rzarzyns@redhat.com>
Wed, 13 Mar 2019 00:23:09 +0000 (01:23 +0100)
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 <rzarzyns@redhat.com>
src/include/types.h

index 92a30f5624864338a57f40db6c1ef206e7d720d3..04c62e3897407f1ae396395e197e84220cb25343 100644 (file)
@@ -561,6 +561,8 @@ WRITE_CMP_OPERATORS_1(errorcode32_t, code)
 template <uint8_t S>
 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<unsigned char, SIZE> tmparr;
+    memcpy(tmparr.data(), v, SIZE);
+    encode(tmparr, bl);
+  }
+  void decode(bufferlist::const_iterator &bl) {
+    using ceph::decode;
+    std::array<unsigned char, SIZE> tmparr;
+    decode(tmparr, bl);
+    memcpy(v, tmparr.data(), SIZE);
+  }
 };
 
 template <uint8_t S>
@@ -582,6 +605,10 @@ inline ostream &operator<<(ostream &out, const sha_digest_t<S> &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