diff options
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/MD5.cpp | 14 | ||||
-rw-r--r-- | llvm/lib/Support/Path.cpp | 35 |
2 files changed, 42 insertions, 7 deletions
diff --git a/llvm/lib/Support/MD5.cpp b/llvm/lib/Support/MD5.cpp index 809dbbce708..bdbf1d67793 100644 --- a/llvm/lib/Support/MD5.cpp +++ b/llvm/lib/Support/MD5.cpp @@ -261,10 +261,16 @@ void MD5::final(MD5Result &Result) { support::endian::write32le(&Result[12], d); } -void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) { +SmallString<32> MD5::MD5Result::digest() const { + SmallString<32> Str; raw_svector_ostream Res(Str); for (int i = 0; i < 16; ++i) - Res << format("%.2x", Result[i]); + Res << format("%.2x", Bytes[i]); + return Str; +} + +void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) { + Str = Result.digest(); } std::array<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) { @@ -273,7 +279,5 @@ std::array<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) { MD5::MD5Result Res; Hash.final(Res); - std::array<uint8_t, 16> Arr; - memcpy(Arr.data(), Res, sizeof(Res)); - return Arr; + return Res; } diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index ffb8ab22088..9fd6652ce4b 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -11,13 +11,14 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Support/Path.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/Support/COFF.h" -#include "llvm/Support/MachO.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Errc.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/Path.h" +#include "llvm/Support/MachO.h" #include "llvm/Support/Process.h" #include <cctype> #include <cstring> @@ -924,6 +925,36 @@ std::error_code copy_file(const Twine &From, const Twine &To) { return std::error_code(); } +ErrorOr<MD5::MD5Result> md5_contents(int FD) { + MD5 Hash; + + constexpr size_t BufSize = 4096; + std::vector<uint8_t> Buf(BufSize); + int BytesRead = 0; + for (;;) { + BytesRead = read(FD, Buf.data(), BufSize); + if (BytesRead <= 0) + break; + Hash.update(makeArrayRef(Buf.data(), BytesRead)); + } + + if (BytesRead < 0) + return std::error_code(errno, std::generic_category()); + MD5::MD5Result Result; + Hash.final(Result); + return Result; +} + +ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) { + int FD; + if (auto EC = openFileForRead(Path, FD)) + return EC; + + auto Result = md5_contents(FD); + close(FD); + return Result; +} + bool exists(file_status status) { return status_known(status) && status.type() != file_type::file_not_found; } |