changeset 1554:89ab71a68fcf

New function OrthancPluginBufferCompression() to (un)compress memory buffers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 20 Aug 2015 11:56:42 +0200
parents 7c4b487b3b4a
children d6a93e12b1c1
files NEWS Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/Include/orthanc/OrthancCPlugin.h
diffstat 4 files changed, 133 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Aug 20 10:32:43 2015 +0200
+++ b/NEWS	Thu Aug 20 11:56:42 2015 +0200
@@ -5,6 +5,11 @@
 * Support of "deflate" and "gzip" content-types in HTTP requests
 * Options to validate peers against CA certificates in HTTPS requests
 
+Plugins
+-------
+
+* New function OrthancPluginBufferCompression() to (un)compress memory buffers
+
 Maintenance
 -----------
 
--- a/Plugins/Engine/OrthancPlugins.cpp	Thu Aug 20 10:32:43 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Thu Aug 20 11:56:42 2015 +0200
@@ -41,6 +41,8 @@
 #include "../../OrthancServer/OrthancInitialization.h"
 #include "../../OrthancServer/ServerContext.h"
 #include "../../OrthancServer/ServerToolbox.h"
+#include "../../Core/Compression/ZlibCompressor.h"
+#include "../../Core/Compression/GzipCompressor.h"
 
 #include <boost/regex.hpp> 
 
@@ -854,6 +856,64 @@
   }
 
 
+  void OrthancPlugins::BufferCompression(const void* parameters)
+  {
+    const _OrthancPluginBufferCompression& p = 
+      *reinterpret_cast<const _OrthancPluginBufferCompression*>(parameters);
+
+    std::string result;
+
+    {
+      std::auto_ptr<DeflateBaseCompressor> compressor;
+
+      switch (p.compression)
+      {
+        case OrthancPluginCompressionType_Zlib:
+        {
+          compressor.reset(new ZlibCompressor);
+          compressor->SetPrefixWithUncompressedSize(false);
+          break;
+        }
+
+        case OrthancPluginCompressionType_ZlibWithSize:
+        {
+          compressor.reset(new ZlibCompressor);
+          compressor->SetPrefixWithUncompressedSize(true);
+          break;
+        }
+
+        case OrthancPluginCompressionType_Gzip:
+        {
+          compressor.reset(new GzipCompressor);
+          compressor->SetPrefixWithUncompressedSize(false);
+          break;
+        }
+
+        case OrthancPluginCompressionType_GzipWithSize:
+        {
+          compressor.reset(new GzipCompressor);
+          compressor->SetPrefixWithUncompressedSize(true);
+          break;
+        }
+
+        default:
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
+      }
+
+      if (p.uncompress)
+      {
+        compressor->Uncompress(result, p.source, p.size);
+      }
+      else
+      {
+        compressor->Compress(result, p.source, p.size);
+      }
+    }
+
+    CopyToMemoryBuffer(*p.target, result);
+  }
+
+
   bool OrthancPlugins::InvokeService(_OrthancPluginService service,
                                      const void* parameters)
   {
@@ -891,6 +951,10 @@
         return true;
       }
 
+      case _OrthancPluginService_BufferCompression:
+        BufferCompression(parameters);
+        return true;
+
       case _OrthancPluginService_RegisterRestCallback:
         RegisterRestCallback(parameters);
         return true;
--- a/Plugins/Engine/OrthancPlugins.h	Thu Aug 20 10:32:43 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.h	Thu Aug 20 11:56:42 2015 +0200
@@ -93,6 +93,8 @@
 
     void SetHttpHeader(const void* parameters);
 
+    void BufferCompression(const void* parameters);
+
   public:
     OrthancPlugins();
 
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Aug 20 10:32:43 2015 +0200
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Aug 20 11:56:42 2015 +0200
@@ -91,7 +91,7 @@
 
 #define ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER     0
 #define ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER     9
-#define ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER  1
+#define ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER  4
 
 
 
@@ -258,6 +258,7 @@
     _OrthancPluginService_GetCommandLineArgument = 11,
     _OrthancPluginService_GetExpectedDatabaseVersion = 12,
     _OrthancPluginService_GetConfiguration = 13,
+    _OrthancPluginService_BufferCompression = 14,
 
     /* Registration of callbacks */
     _OrthancPluginService_RegisterRestCallback = 1000,
@@ -379,7 +380,7 @@
 
 
   /**
-   * The supported type of DICOM resources.
+   * The supported types of DICOM resources.
    **/
   typedef enum
   {
@@ -392,7 +393,7 @@
 
 
   /**
-   * The supported type of changes that can happen to DICOM resources.
+   * The supported types of changes that can happen to DICOM resources.
    **/
   typedef enum
   {
@@ -409,6 +410,18 @@
   } OrthancPluginChangeType;
 
 
+  /**
+   * The compression algorithms that are known by the Orthanc core.
+   **/
+  typedef enum
+  {
+    OrthancPluginCompressionType_Zlib = 0,          /*!< Standard zlib compression */
+    OrthancPluginCompressionType_ZlibWithSize = 1,  /*!< zlib, prefixed with uncompressed size (uint64_t) */
+    OrthancPluginCompressionType_Gzip = 2,          /*!< Standard gzip compression */
+    OrthancPluginCompressionType_GzipWithSize = 3   /*!< gzip, prefixed with uncompressed size (uint64_t) */
+  } OrthancPluginCompressionType;
+
+
 
   /**
    * @brief A memory buffer allocated by the core system of Orthanc.
@@ -2216,6 +2229,52 @@
     return context->InvokeService(context, _OrthancPluginService_SendMultipartItem, &params);
   }
 
+
+
+  typedef struct
+  {
+    OrthancPluginMemoryBuffer*    target;
+    const void*                   source;
+    uint32_t                      size;
+    OrthancPluginCompressionType  compression;
+    uint8_t                       uncompress;
+  } _OrthancPluginBufferCompression;
+
+
+  /**
+   * @brief Compress or decompress a buffer.
+   *
+   * This function compresses or decompresses a buffer, using the
+   * version of the zlib library that is used by the Orthanc core.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param target The target memory buffer.
+   * @param source The source buffer.
+   * @param size The size in bytes of the source buffer.
+   * @param compression The compression algorithm.
+   * @param uncompress If set to "0", the buffer must be compressed. 
+   * If set to "1", the buffer must be uncompressed.
+   * @return 0 if success, other value if error.
+   **/
+  ORTHANC_PLUGIN_INLINE int32_t OrthancPluginBufferCompression(
+    OrthancPluginContext*         context,
+    OrthancPluginMemoryBuffer*    target,
+    const void*                   source,
+    uint32_t                      size,
+    OrthancPluginCompressionType  compression,
+    uint8_t                       uncompress)
+  {
+    _OrthancPluginBufferCompression params;
+    params.target = target;
+    params.source = source;
+    params.size = size;
+    params.compression = compression;
+    params.uncompress = uncompress;
+
+    return context->InvokeService(context, _OrthancPluginService_BufferCompression, &params);
+  }
+
+
 #ifdef  __cplusplus
 }
 #endif