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 <algorithm> 15 #include <fstream> 16 #include <vector> 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 namespace { 22 23 bool CalcMD5(const FileSpec &file_spec, uint64_t offset, uint64_t length, 24 llvm::MD5::MD5Result &md5_result) { 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 if (offset > 0) 31 file.seekg(offset, file.beg); 32 33 std::vector<char> read_buf(4096); 34 uint64_t total_read_bytes = 0; 35 while (!file.eof()) { 36 const uint64_t to_read = 37 (length > 0) ? std::min(static_cast<uint64_t>(read_buf.size()), 38 length - total_read_bytes) 39 : read_buf.size(); 40 if (to_read == 0) 41 break; 42 43 file.read(&read_buf[0], to_read); 44 const auto read_bytes = file.gcount(); 45 if (read_bytes == 0) 46 break; 47 48 md5_hash.update(llvm::StringRef(&read_buf[0], read_bytes)); 49 total_read_bytes += read_bytes; 50 } 51 52 md5_hash.final(md5_result); 53 return true; 54 } 55 56 } // namespace 57 58 bool FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, 59 uint64_t &high) { 60 return CalculateMD5(file_spec, 0, 0, low, high); 61 } 62 63 bool FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t offset, 64 uint64_t length, uint64_t &low, uint64_t &high) { 65 llvm::MD5::MD5Result md5_result; 66 if (!CalcMD5(file_spec, offset, length, md5_result)) 67 return false; 68 69 const auto uint64_res = reinterpret_cast<const uint64_t *>(md5_result); 70 high = uint64_res[0]; 71 low = uint64_res[1]; 72 73 return true; 74 } 75 76 bool FileSystem::CalculateMD5AsString(const FileSpec &file_spec, 77 std::string &digest_str) { 78 return CalculateMD5AsString(file_spec, 0, 0, digest_str); 79 } 80 81 bool FileSystem::CalculateMD5AsString(const FileSpec &file_spec, 82 uint64_t offset, uint64_t length, 83 std::string &digest_str) { 84 llvm::MD5::MD5Result md5_result; 85 if (!CalcMD5(file_spec, offset, length, md5_result)) 86 return false; 87 88 llvm::SmallString<32> result_str; 89 llvm::MD5::stringifyResult(md5_result, result_str); 90 digest_str = result_str.c_str(); 91 return true; 92 } 93