[Lldb-commits] [PATCH] Introduce FileSystem::CalculateMD5AsString that supports any platform and make existing FileSystem::CalculateMD5 to use it.

Greg Clayton clayborg at gmail.com
Thu Feb 19 15:23:59 PST 2015

Move the code that calculates the MD5 into a static function that both FileSystem::CalculateMD5AsString() and FileSystem::CalculateMD5() use. See inlined comments for details.

Comment at: include/lldb/Host/FileSystem.h:40
@@ -39,2 +39,3 @@
     static bool CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high);
+    static bool CalculateMD5AsString(const FileSpec &file_spec, std::string& digest_str);
I would rather not move this to LLVM just yet. We merge the top of tree SVN to many other branches internally here at apple and these internal branches link against older versions of llvm/clang that won't have this new functionality, so please leave this in FileSystem for now.

Comment at: source/Host/common/FileSystem.cpp:38-58
@@ +37,23 @@
+FileSystem::CalculateMD5AsString(const FileSpec &file_spec, std::string& digest_str)
+    llvm::MD5 md5_hash;
+    std::ifstream file(file_spec.GetPath(), std::ios::binary);
+    if (!file.is_open())
+        return false;
+    char read_buf[4096];
+    while (!file.eof())
+    {
+        file.read(read_buf, sizeof(read_buf));
+        const auto read_bytes = file.gcount();
+        if (read_bytes == 0)
+            break;
+        md5_hash.update(llvm::StringRef(read_buf, read_bytes));
+    }
+    llvm::MD5::MD5Result md5_result;
+    md5_hash.final(md5_result);
+    llvm::SmallString<32> result_str;
Move the MD5 calculation into a static function:

CalculateMD5 (const FileSpec &file_spec, llvm::MD5::MD5Result &md5_result);

And have both versions use it. The version that wants to return two uint64 values should be able to cast the llvm::MD5::MD5Result to a "uint64_t *' and access both numbers and it then avoids creating a string and calling strtoull on the two strings.



More information about the lldb-commits mailing list