about summary refs log tree commit diff
path: root/third_party
diff options
context:
space:
mode:
Diffstat (limited to 'third_party')
-rw-r--r--third_party/nix/src/libutil/hash.cc27
-rw-r--r--third_party/nix/src/libutil/hash.hh8
2 files changed, 18 insertions, 17 deletions
diff --git a/third_party/nix/src/libutil/hash.cc b/third_party/nix/src/libutil/hash.cc
index 50169b0f19c7..e9901d0ccb94 100644
--- a/third_party/nix/src/libutil/hash.cc
+++ b/third_party/nix/src/libutil/hash.cc
@@ -280,6 +280,8 @@ absl::StatusOr<Hash> Hash::deserialize(const std::string& s, HashType type) {
   return dest;
 }
 
+namespace hash {
+
 union Ctx {
   MD5_CTX md5;
   SHA_CTX sha1;
@@ -324,8 +326,10 @@ static void finish(HashType ht, Ctx& ctx, unsigned char* hash) {
   }
 }
 
+}  // namespace hash
+
 Hash hashString(HashType ht, const std::string& s) {
-  Ctx ctx{};
+  hash::Ctx ctx{};
   Hash hash(ht);
   start(ht, ctx);
   update(ht, ctx, reinterpret_cast<const unsigned char*>(s.data()), s.length());
@@ -334,7 +338,7 @@ Hash hashString(HashType ht, const std::string& s) {
 }
 
 Hash hashFile(HashType ht, const Path& path) {
-  Ctx ctx{};
+  hash::Ctx ctx{};
   Hash hash(ht);
   start(ht, ctx);
 
@@ -357,34 +361,27 @@ Hash hashFile(HashType ht, const Path& path) {
   return hash;
 }
 
-HashSink::HashSink(HashType ht) : ht(ht) {
-  ctx = new Ctx;
-  bytes = 0;
-  start(ht, *ctx);
-}
+HashSink::HashSink(HashType ht) : ht(ht), ctx(), bytes(0) { start(ht, *ctx); }
 
-HashSink::~HashSink() {
-  bufPos = 0;
-  delete ctx;
-}
+HashSink::~HashSink() { bufPos = 0; }
 
 void HashSink::write(const unsigned char* data, size_t len) {
   bytes += len;
-  update(ht, *ctx, data, len);
+  nix::hash::update(ht, *ctx, data, len);
 }
 
 HashResult HashSink::finish() {
   flush();
   Hash hash(ht);
-  nix::finish(ht, *ctx, hash.hash);
+  nix::hash::finish(ht, *ctx, hash.hash);
   return HashResult(hash, bytes);
 }
 
 HashResult HashSink::currentHash() {
   flush();
-  Ctx ctx2 = *ctx;
+  nix::hash::Ctx ctx2 = *ctx;
   Hash hash(ht);
-  nix::finish(ht, ctx2, hash.hash);
+  nix::hash::finish(ht, ctx2, hash.hash);
   return HashResult(hash, bytes);
 }
 
diff --git a/third_party/nix/src/libutil/hash.hh b/third_party/nix/src/libutil/hash.hh
index 0b7b11edd072..208615f67bc8 100644
--- a/third_party/nix/src/libutil/hash.hh
+++ b/third_party/nix/src/libutil/hash.hh
@@ -88,9 +88,11 @@ Hash hashString(HashType ht, const std::string& s);
 /* Compute the hash of the given file. */
 Hash hashFile(HashType ht, const Path& path);
 
+/* A pair of the Hash, and the number of bytes consumed. */
+typedef std::pair<Hash, unsigned long long> HashResult;
+
 /* Compute the hash of the given path.  The hash is defined as
    (essentially) hashString(ht, dumpPath(path)). */
-typedef std::pair<Hash, unsigned long long> HashResult;
 HashResult hashPath(HashType ht, const Path& path,
                     PathFilter& filter = defaultPathFilter);
 
@@ -104,12 +106,14 @@ HashType parseHashType(const std::string& s);
 /* And the reverse. */
 std::string printHashType(HashType ht);
 
+namespace hash {
 union Ctx;
+}
 
 class HashSink : public BufferedSink {
  private:
   HashType ht;
-  Ctx* ctx;
+  std::unique_ptr<hash::Ctx> ctx;
   unsigned long long bytes;
 
  public: