# HG changeset patch # User Sebastien Jodogne # Date 1748599244 -7200 # Node ID 98776c72a9bc3043350335933b60881d8ca30a93 # Parent e030b8efe0196c878405ab47e1ad716b6505d24e fix MD5 hashes for large memory buffers diff -r e030b8efe019 -r 98776c72a9bc NEWS --- a/NEWS Fri May 30 11:38:18 2025 +0200 +++ b/NEWS Fri May 30 12:00:44 2025 +0200 @@ -22,6 +22,7 @@ - If "LimitMainDicomTagsReconstructLevel" was set, files were not transcoded if they had to. The "LimitMainDicomTagsReconstructLevel" configuration is now ignored when a full processing is required. +* Fix computation of MD5 hashes for memory buffers whose size is larger than 2^31 bytes. Version 1.12.7 (2025-04-07) diff -r e030b8efe019 -r 98776c72a9bc OrthancFramework/Sources/Toolbox.cpp --- a/OrthancFramework/Sources/Toolbox.cpp Fri May 30 11:38:18 2025 +0200 +++ b/OrthancFramework/Sources/Toolbox.cpp Fri May 30 12:00:44 2025 +0200 @@ -64,6 +64,7 @@ #include #include #include +#include #if BOOST_VERSION >= 106600 # include @@ -245,22 +246,39 @@ void Toolbox::MD5Context::Append(const void* data, size_t size) { + static const size_t MAX_SIZE = 128 * 1024 * 1024; + if (pimpl_->done_) { throw OrthancException(ErrorCode_BadSequenceOfCalls); } - if (static_cast(static_cast(size)) != size) + const uint8_t *p = reinterpret_cast(data); + + while (size > 0) { - throw OrthancException(ErrorCode_InternalError, - "The built-in implementation of MD5 does not support buffers larger than 32bits"); - } - - if (size > 0) - { - md5_append(&pimpl_->state_, - reinterpret_cast(data), - static_cast(size)); + /** + * The built-in implementation of MD5 requires that "size" can + * be casted to "int", so we feed it by chunks of maximum + * 128MB. This fixes an incorrect behavior in Orthanc <= 1.12.7. + **/ + + int chunkSize; + if (size > MAX_SIZE) + { + chunkSize = static_cast(MAX_SIZE); + } + else + { + chunkSize = static_cast(size); + } + + md5_append(&pimpl_->state_, reinterpret_cast(p), chunkSize); + + p += chunkSize; + + assert(static_cast(chunkSize) <= size); + size -= chunkSize; } }