# HG changeset patch # User Alain Mazy # Date 1747855874 -7200 # Node ID 24bf08f02e55ecb5bd43b281949fda620205685f # Parent 31d41a52ac2f8d8d5640684b46793e6e3757df9b# Parent 08745ddaee1287cdc4d06637849b988623293e40 merge diff -r 08745ddaee12 -r 24bf08f02e55 OrthancFramework/Sources/SystemToolbox.cpp --- a/OrthancFramework/Sources/SystemToolbox.cpp Wed May 21 17:24:19 2025 +0200 +++ b/OrthancFramework/Sources/SystemToolbox.cpp Wed May 21 21:31:14 2025 +0200 @@ -454,6 +454,31 @@ } } +#if ORTHANC_ENABLE_MD5 == 1 + void SystemToolbox::ComputeFileMD5(std::string& result, + const std::string& path) + { + std::ifstream fileStream(path, std::ifstream::binary); + Toolbox::ComputeMD5(result, fileStream); + } + + bool SystemToolbox::CompareFilesMD5(const std::string& path1, + const std::string& path2) + { + if (SystemToolbox::GetFileSize(path1) != SystemToolbox::GetFileSize(path2)) + { + return false; + } + + std::string path1md5, path2md5; + + SystemToolbox::ComputeFileMD5(path1md5, path1); + SystemToolbox::ComputeFileMD5(path2md5, path2); + + return path1md5 == path2md5; + } +#endif + void SystemToolbox::MakeDirectory(const std::string& path) { diff -r 08745ddaee12 -r 24bf08f02e55 OrthancFramework/Sources/SystemToolbox.h --- a/OrthancFramework/Sources/SystemToolbox.h Wed May 21 17:24:19 2025 +0200 +++ b/OrthancFramework/Sources/SystemToolbox.h Wed May 21 21:31:14 2025 +0200 @@ -83,6 +83,15 @@ static uint64_t GetFileSize(const std::string& path); +#if ORTHANC_ENABLE_MD5 == 1 + static void ComputeFileMD5(std::string& result, + const std::string& path); + + // returns true if file have the same MD5 + static bool CompareFilesMD5(const std::string& path1, + const std::string& path2); +#endif + static void MakeDirectory(const std::string& path); static bool IsExistingFile(const std::string& path); diff -r 08745ddaee12 -r 24bf08f02e55 OrthancFramework/Sources/Toolbox.cpp --- a/OrthancFramework/Sources/Toolbox.cpp Wed May 21 17:24:19 2025 +0200 +++ b/OrthancFramework/Sources/Toolbox.cpp Wed May 21 21:31:14 2025 +0200 @@ -499,6 +499,40 @@ } void Toolbox::ComputeMD5(std::string& result, + std::istream& inputStream) + { + md5_state_s state; + md5_init(&state); + + const size_t bufferSize = 1024; + char buffer[bufferSize]; + + while (inputStream.good()) + { + inputStream.read(buffer, bufferSize); + std::streamsize bytesRead = inputStream.gcount(); + + if (bytesRead > 0) + { + md5_append(&state, + reinterpret_cast(buffer), + static_cast(bytesRead)); + } + } + + md5_byte_t actualHash[16]; + md5_finish(&state, actualHash); + + result.resize(32); + for (unsigned int i = 0; i < 16; i++) + { + result[2 * i] = GetHexadecimalCharacter(static_cast(actualHash[i] / 16)); + result[2 * i + 1] = GetHexadecimalCharacter(static_cast(actualHash[i] % 16)); + } + } + + + void Toolbox::ComputeMD5(std::string& result, const std::set& data) { std::string s; diff -r 08745ddaee12 -r 24bf08f02e55 OrthancFramework/Sources/Toolbox.h --- a/OrthancFramework/Sources/Toolbox.h Wed May 21 17:24:19 2025 +0200 +++ b/OrthancFramework/Sources/Toolbox.h Wed May 21 21:31:14 2025 +0200 @@ -134,6 +134,9 @@ static void ComputeMD5(std::string& result, const std::set& data); + + static void ComputeMD5(std::string& result, + std::istream& stream); #endif static void ComputeSHA1(std::string& result, diff -r 08745ddaee12 -r 24bf08f02e55 OrthancFramework/UnitTestsSources/FrameworkTests.cpp --- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp Wed May 21 17:24:19 2025 +0200 +++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp Wed May 21 21:31:14 2025 +0200 @@ -397,6 +397,10 @@ Toolbox::ComputeMD5(s, set); ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s); // set md5 same as string with the values sorted + + std::istringstream iss(std::string("aaabbbccc")); + Toolbox::ComputeMD5(s, iss); + ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s); } TEST(Toolbox, ComputeSHA1) @@ -1591,6 +1595,50 @@ #endif +#if ORTHANC_SANDBOXED != 1 && ORTHANC_ENABLE_MD5 == 1 +TEST(Toolbox, FileMD5) +{ + std::string path; + + { + TemporaryFile tmp1, tmp2; + std::string s = "aaabbbccc"; + + SystemToolbox::WriteFile(s, tmp1.GetPath()); + SystemToolbox::WriteFile(s, tmp2.GetPath()); + + std::string md5; + SystemToolbox::ComputeFileMD5(md5, tmp1.GetPath()); + + ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", md5); + ASSERT_TRUE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath())); + } + + { // different sizes + TemporaryFile tmp1, tmp2; + std::string s1 = "aaabbbccc"; + std::string s2 = "aaabbbcccd"; + + SystemToolbox::WriteFile(s1, tmp1.GetPath()); + SystemToolbox::WriteFile(s2, tmp2.GetPath()); + + ASSERT_FALSE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath())); + } + + { // same sizes, different contents + TemporaryFile tmp1, tmp2; + std::string s1 = "aaabbbccc"; + std::string s2 = "aaabbbccd"; + + SystemToolbox::WriteFile(s1, tmp1.GetPath()); + SystemToolbox::WriteFile(s2, tmp2.GetPath()); + + ASSERT_FALSE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath())); + } + +} +#endif + #if ORTHANC_SANDBOXED != 1 TEST(Toolbox, GetMacAddressess) { diff -r 08745ddaee12 -r 24bf08f02e55 OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp --- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Wed May 21 17:24:19 2025 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Wed May 21 21:31:14 2025 +0200 @@ -2128,6 +2128,7 @@ dbCapabilities_.SetHasFindSupport(systemInfo.supports_find()); dbCapabilities_.SetKeyValueStoresSupport(systemInfo.supports_key_value_stores()); dbCapabilities_.SetQueuesSupport(systemInfo.supports_queues()); + dbCapabilities_.SetAttachmentCustomDataSupport(systemInfo.has_attachment_custom_data()); } open_ = true; diff -r 08745ddaee12 -r 24bf08f02e55 OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto --- a/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto Wed May 21 17:24:19 2025 +0200 +++ b/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto Wed May 21 21:31:14 2025 +0200 @@ -174,6 +174,7 @@ bool has_extended_changes = 9; // New in Orthanc 1.12.5 bool supports_key_value_stores = 10; // New in Orthanc 1.12.99 bool supports_queues = 11; // New in Orthanc 1.12.99 + bool has_attachment_custom_data = 12; // New in Orthanc 1.12.99 } } diff -r 08745ddaee12 -r 24bf08f02e55 OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp --- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp Wed May 21 17:24:19 2025 +0200 +++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp Wed May 21 21:31:14 2025 +0200 @@ -2516,6 +2516,7 @@ dbCapabilities_.SetHasFindSupport(HasIntegratedFind()); dbCapabilities_.SetKeyValueStoresSupport(true); dbCapabilities_.SetQueuesSupport(true); + dbCapabilities_.SetAttachmentCustomDataSupport(true); db_.Open(path); } @@ -2532,6 +2533,7 @@ dbCapabilities_.SetHasFindSupport(HasIntegratedFind()); dbCapabilities_.SetKeyValueStoresSupport(true); dbCapabilities_.SetQueuesSupport(true); + dbCapabilities_.SetAttachmentCustomDataSupport(true); db_.OpenInMemory(); }