summaryrefslogtreecommitdiffstats
path: root/lldb/source
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/source')
-rw-r--r--lldb/source/Host/CMakeLists.txt1
-rw-r--r--lldb/source/Host/common/FileSystem.cpp72
-rw-r--r--lldb/source/Host/posix/FileSystem.cpp27
-rw-r--r--lldb/source/Host/windows/FileSystem.cpp6
4 files changed, 73 insertions, 33 deletions
diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt
index 19f95b29066..cdc79bcd954 100644
--- a/lldb/source/Host/CMakeLists.txt
+++ b/lldb/source/Host/CMakeLists.txt
@@ -8,6 +8,7 @@ add_host_subdirectory(common
common/File.cpp
common/FileCache.cpp
common/FileSpec.cpp
+ common/FileSystem.cpp
common/Host.cpp
common/HostInfoBase.cpp
common/HostNativeThreadBase.cpp
diff --git a/lldb/source/Host/common/FileSystem.cpp b/lldb/source/Host/common/FileSystem.cpp
new file mode 100644
index 00000000000..509a187e40e
--- /dev/null
+++ b/lldb/source/Host/common/FileSystem.cpp
@@ -0,0 +1,72 @@
+//===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/FileSystem.h"
+
+#include "llvm/Support/MD5.h"
+
+#include <fstream>
+#include <vector>
+
+using namespace lldb;
+using namespace lldb_private;
+
+namespace {
+
+bool
+CalcMD5(const FileSpec &file_spec, llvm::MD5::MD5Result &md5_result)
+{
+ llvm::MD5 md5_hash;
+ std::ifstream file(file_spec.GetPath(), std::ios::binary);
+ if (!file.is_open())
+ return false;
+
+ std::vector<char> read_buf(4096);
+ while (!file.eof())
+ {
+ file.read(&read_buf[0], read_buf.size());
+ const auto read_bytes = file.gcount();
+ if (read_bytes == 0)
+ break;
+
+ md5_hash.update(llvm::StringRef(&read_buf[0], read_bytes));
+ }
+
+ md5_hash.final(md5_result);
+ return true;
+}
+
+} // namespace
+
+bool
+FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high)
+{
+ llvm::MD5::MD5Result md5_result;
+ if (!CalcMD5(file_spec, md5_result))
+ return false;
+
+ const auto uint64_res = reinterpret_cast<const uint64_t*>(md5_result);
+ high = uint64_res[0];
+ low = uint64_res[1];
+
+ return true;
+}
+
+bool
+FileSystem::CalculateMD5AsString(const FileSpec &file_spec, std::string& digest_str)
+{
+ llvm::MD5::MD5Result md5_result;
+ if (!CalcMD5(file_spec, md5_result))
+ return false;
+
+ llvm::SmallString<32> result_str;
+ llvm::MD5::stringifyResult(md5_result, result_str);
+ digest_str = result_str.c_str();
+ return true;
+}
diff --git a/lldb/source/Host/posix/FileSystem.cpp b/lldb/source/Host/posix/FileSystem.cpp
index 57131681114..b55b01b4569 100644
--- a/lldb/source/Host/posix/FileSystem.cpp
+++ b/lldb/source/Host/posix/FileSystem.cpp
@@ -172,30 +172,3 @@ FileSystem::Readlink(const char *path, char *buf, size_t buf_len)
error.SetErrorString("'buf' buffer is too small to contain link contents");
return error;
}
-
-bool
-FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high)
-{
-#if defined(__APPLE__)
- StreamString md5_cmd_line;
- md5_cmd_line.Printf("md5 -q '%s'", file_spec.GetPath().c_str());
- std::string hash_string;
- Error err = Host::RunShellCommand(md5_cmd_line.GetData(), NULL, NULL, NULL, &hash_string, 60);
- if (err.Fail())
- return false;
- // a correctly formed MD5 is 16-bytes, that is 32 hex digits
- // if the output is any other length it is probably wrong
- if (hash_string.size() != 32)
- return false;
- std::string part1(hash_string, 0, 16);
- std::string part2(hash_string, 16);
- const char *part1_cstr = part1.c_str();
- const char *part2_cstr = part2.c_str();
- high = ::strtoull(part1_cstr, NULL, 16);
- low = ::strtoull(part2_cstr, NULL, 16);
- return true;
-#else
- // your own MD5 implementation here
- return false;
-#endif
-}
diff --git a/lldb/source/Host/windows/FileSystem.cpp b/lldb/source/Host/windows/FileSystem.cpp
index f641ff59f97..a58ddef13ad 100644
--- a/lldb/source/Host/windows/FileSystem.cpp
+++ b/lldb/source/Host/windows/FileSystem.cpp
@@ -138,9 +138,3 @@ FileSystem::Readlink(const char *path, char *buf, size_t buf_len)
::CloseHandle(h);
return error;
}
-
-bool
-FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high)
-{
- return false;
-}
OpenPOWER on IntegriCloud