From 7eeeeaa625997c72d55ef0e13dec54ff2f8228b2 Mon Sep 17 00:00:00 2001
From: Philip Rebohle <philip.rebohle@tu-dortmund.de>
Date: Thu, 25 Oct 2018 10:33:48 +0200
Subject: [PATCH] [util] Provide method to compute SHA-1 hash from multiple
 data chunks

The underlying implementation supports this trivially, so we should
provide a way to use this feature.
---
 src/util/sha1/sha1_util.cpp | 17 +++++++++++++++--
 src/util/sha1/sha1_util.h   | 15 +++++++++++----
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/util/sha1/sha1_util.cpp b/src/util/sha1/sha1_util.cpp
index 7be5e9ee..eefc9275 100644
--- a/src/util/sha1/sha1_util.cpp
+++ b/src/util/sha1/sha1_util.cpp
@@ -21,13 +21,26 @@ namespace dxvk {
   
   
   Sha1Hash Sha1Hash::compute(
-    const uint8_t*  data,
+    const void*     data,
           size_t    size) {
+    Sha1Data chunk = { data, size };
+    return compute(1, &chunk);
+  }
+  
+  
+  Sha1Hash Sha1Hash::compute(
+          size_t    numChunks,
+    const Sha1Data* chunks) {
     Sha1Digest digest;
     
     SHA1_CTX ctx;
     SHA1Init(&ctx);
-    SHA1Update(&ctx, data, size);
+    
+    for (size_t i = 0; i < numChunks; i++) {
+      auto ptr = reinterpret_cast<const uint8_t*>(chunks[i].data);
+      SHA1Update(&ctx, ptr, chunks[i].size);
+    }
+
     SHA1Final(digest.data(), &ctx);
     return Sha1Hash(digest);
   }
diff --git a/src/util/sha1/sha1_util.h b/src/util/sha1/sha1_util.h
index ae09323b..a9c85fa4 100644
--- a/src/util/sha1/sha1_util.h
+++ b/src/util/sha1/sha1_util.h
@@ -7,6 +7,11 @@
 namespace dxvk {
   
   using Sha1Digest = std::array<uint8_t, 20>;
+
+  struct Sha1Data {
+    const void* data;
+    size_t      size;
+  };
   
   class Sha1Hash {
     
@@ -33,16 +38,18 @@ namespace dxvk {
     }
     
     static Sha1Hash compute(
-      const uint8_t*  data,
+      const void*     data,
             size_t    size);
     
+    static Sha1Hash compute(
+            size_t    numChunks,
+      const Sha1Data* chunks);
+    
     template<typename T>
     static Sha1Hash compute(const T& data) {
-      auto bytes = reinterpret_cast<const uint8_t*>(&data);
-      return compute(bytes, sizeof(T));
+      return compute(&data, sizeof(T));
     }
 
-    
   private:
     
     Sha1Digest m_digest;