changeset 6806:68d9bad29411

added StorageAreaDataSource::Range::Copy()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 19 May 2026 10:52:00 +0200
parents 91cb6f565e7d
children 6df5cb04dd7f
files OrthancFramework/Sources/DataSource/DicomDataSource.cpp OrthancFramework/Sources/DataSource/DicomDataSource.h OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp OrthancFramework/Sources/DataSource/StorageAreaDataSource.h
diffstat 4 files changed, 45 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.cpp	Tue May 19 07:28:08 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomDataSource.cpp	Tue May 19 10:52:00 2026 +0200
@@ -35,6 +35,28 @@
 
 namespace Orthanc
 {
+  static size_t ComputeValueSize(size_t size,
+                                 bool isKeepRawBuffer)
+  {
+    if (isKeepRawBuffer)
+    {
+      uint64_t s = 2 * static_cast<uint64_t>(size);
+      if (s != static_cast<uint64_t>(static_cast<size_t>(s)))
+      {
+        return std::numeric_limits<size_t>::max();
+      }
+      else
+      {
+        return static_cast<size_t>(s);
+      }
+    }
+    else
+    {
+      return size;
+    }
+  }
+
+
   class DicomDataSource::BaseIdentifier : public IDataIdentifier
   {
   public:
@@ -71,15 +93,7 @@
 
     virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE
     {
-      if (keepRawBuffer_)
-      {
-        target = 2 * attachment_.GetUncompressedSize();
-      }
-      else
-      {
-        target = attachment_.GetUncompressedSize();
-      }
-
+      target = ComputeValueSize(attachment_.GetUncompressedSize(), keepRawBuffer_);
       return true;
     }
 
@@ -225,19 +239,12 @@
     const size_t size = range->GetSize();
 
     {
-    // Sanity check
+      // Sanity check
       bool ok = false;
       size_t estimatedSize;
       if (obj.EstimateValueSize(estimatedSize))
       {
-        if (id.IsKeepRawBuffer())
-        {
-          ok = (estimatedSize == 2 * size);
-        }
-        else
-        {
-          ok = (estimatedSize == size);
-        }
+        ok = (estimatedSize == ComputeValueSize(size, id.IsKeepRawBuffer()));
       }
 
       if (!ok)
@@ -265,15 +272,7 @@
   size_t DicomDataSource::GetValueSize(const IDynamicObject& obj) const
   {
     const Value& value = dynamic_cast<const Value&>(obj);
-
-    if (value.HasRawBuffer())
-    {
-      return 2 * value.GetRawBufferSize();
-    }
-    else
-    {
-      return value.GetRawBufferSize();
-    }
+    return ComputeValueSize(value.GetRawBufferSize(), value.HasRawBuffer());
   }
 
 
@@ -324,9 +323,14 @@
 
   DicomDataSource::Dicom* DicomDataSource::ReadUntilPixelData(DataSourceReader& reader,
                                                               const FileInfo& attachment,
-                                                              size_t pixelDataOffset)
+                                                              uint64_t pixelDataOffset)
   {
-    std::unique_ptr<IDataIdentifier> id(new BeginningIdentifier(attachment, pixelDataOffset));
+    if (static_cast<uint64_t>(static_cast<size_t>(pixelDataOffset)) != pixelDataOffset)
+    {
+      throw OrthancException(ErrorCode_NotEnoughMemory);
+    }
+
+    std::unique_ptr<IDataIdentifier> id(new BeginningIdentifier(attachment, static_cast<size_t>(pixelDataOffset)));
     boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release());
     return new Dicom(value);
   }
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.h	Tue May 19 07:28:08 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomDataSource.h	Tue May 19 10:52:00 2026 +0200
@@ -107,6 +107,6 @@
 
     static Dicom* ReadUntilPixelData(DataSourceReader& reader,
                                      const FileInfo& attachment,
-                                     size_t pixelDataOffset);
+                                     uint64_t pixelDataOffset);
   };
 }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Tue May 19 07:28:08 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Tue May 19 10:52:00 2026 +0200
@@ -162,6 +162,13 @@
   }
 
 
+  void StorageAreaDataSource::Range::Copy(std::string& to) const
+  {
+    const IMemoryBuffer& buffer = GetValue().GetBuffer();
+    to.assign(reinterpret_cast<const char*>(buffer.GetData()), buffer.GetSize());
+  }
+
+
   StorageAreaDataSource::Range* StorageAreaDataSource::ReadRange(DataSourceReader& reader,
                                                                  const std::string& uuid,
                                                                  FileContentType type,
@@ -252,7 +259,7 @@
 
   StorageAreaDataSource::Range* StorageAreaDataSource::ReadBeginning(DataSourceReader& reader,
                                                                      const FileInfo& attachment,
-                                                                     size_t untilPosition)
+                                                                     uint64_t untilPosition)
   {
     if (attachment.GetCompressionType() != CompressionType_None)
     {
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Tue May 19 07:28:08 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Tue May 19 10:52:00 2026 +0200
@@ -72,6 +72,8 @@
       const void* GetData() const;
 
       size_t GetSize() const;
+
+      void Copy(std::string& to) const;
     };
 
     static Range* ReadRange(DataSourceReader& reader,
@@ -88,6 +90,6 @@
 
     static Range* ReadBeginning(DataSourceReader& reader,
                                 const FileInfo& attachment,
-                                size_t untilPosition);
+                                uint64_t untilPosition);
   };
 }