1 //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Host/FileSystem.h" 11 12 #include "llvm/Support/MD5.h" 13 14 #include <fstream> 15 #include <vector> 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 namespace { 21 22 bool 23 CalcMD5(const FileSpec &file_spec, llvm::MD5::MD5Result &md5_result) 24 { 25 llvm::MD5 md5_hash; 26 std::ifstream file(file_spec.GetPath(), std::ios::binary); 27 if (!file.is_open()) 28 return false; 29 30 std::vector<char> read_buf(4096); 31 while (!file.eof()) 32 { 33 file.read(&read_buf[0], read_buf.size()); 34 const auto read_bytes = file.gcount(); 35 if (read_bytes == 0) 36 break; 37 38 md5_hash.update(llvm::StringRef(&read_buf[0], read_bytes)); 39 } 40 41 md5_hash.final(md5_result); 42 return true; 43 } 44 45 } // namespace 46 47 bool 48 FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high) 49 { 50 llvm::MD5::MD5Result md5_result; 51 if (!CalcMD5(file_spec, md5_result)) 52 return false; 53 54 const auto uint64_res = reinterpret_cast<const uint64_t*>(md5_result); 55 high = uint64_res[0]; 56 low = uint64_res[1]; 57 58 return true; 59 } 60 61 bool 62 FileSystem::CalculateMD5AsString(const FileSpec &file_spec, std::string& digest_str) 63 { 64 llvm::MD5::MD5Result md5_result; 65 if (!CalcMD5(file_spec, md5_result)) 66 return false; 67 68 llvm::SmallString<32> result_str; 69 llvm::MD5::stringifyResult(md5_result, result_str); 70 digest_str = result_str.c_str(); 71 return true; 72 } 73