# HG changeset patch # User Sebastien Jodogne # Date 1439211130 -7200 # Node ID 52dc56bcec7d1ad61c74bfd4590fc660a7252d7a # Parent 7962563129c97e62b85d4218f34ccf58ca4c757b refactoring diff -r 7962563129c9 -r 52dc56bcec7d CMakeLists.txt --- a/CMakeLists.txt Mon Aug 10 14:18:24 2015 +0200 +++ b/CMakeLists.txt Mon Aug 10 14:52:10 2015 +0200 @@ -82,10 +82,10 @@ Core/Cache/MemoryCache.cpp Core/Cache/SharedArchive.cpp Core/ChunkedBuffer.cpp - Core/Compression/BufferCompressor.cpp + Core/Compression/DeflateBaseCompressor.cpp + Core/Compression/HierarchicalZipWriter.cpp + Core/Compression/ZipWriter.cpp Core/Compression/ZlibCompressor.cpp - Core/Compression/ZipWriter.cpp - Core/Compression/HierarchicalZipWriter.cpp Core/OrthancException.cpp Core/DicomFormat/DicomArray.cpp Core/DicomFormat/DicomMap.cpp diff -r 7962563129c9 -r 52dc56bcec7d Core/Compression/BufferCompressor.cpp --- a/Core/Compression/BufferCompressor.cpp Mon Aug 10 14:18:24 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -/** - * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics - * Department, University Hospital of Liege, Belgium - * - * This program is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * In addition, as a special exception, the copyright holders of this - * program give permission to link the code of its release with the - * OpenSSL project's "OpenSSL" library (or with modified versions of it - * that use the same license as the "OpenSSL" library), and distribute - * the linked executables. You must obey the GNU General Public License - * in all respects for all of the code used other than "OpenSSL". If you - * modify file(s) with this exception, you may extend this exception to - * your version of the file(s), but you are not obligated to do so. If - * you do not wish to do so, delete this exception statement from your - * version. If you delete this exception statement from all source files - * in the program, then also delete it here. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - **/ - - -#include "../PrecompiledHeaders.h" -#include "BufferCompressor.h" - -namespace Orthanc -{ - void BufferCompressor::Compress(std::string& output, - const std::vector& input) - { - if (input.size() > 0) - Compress(output, &input[0], input.size()); - else - Compress(output, NULL, 0); - } - - void BufferCompressor::Uncompress(std::string& output, - const std::vector& input) - { - if (input.size() > 0) - Uncompress(output, &input[0], input.size()); - else - Uncompress(output, NULL, 0); - } - - void BufferCompressor::Compress(std::string& output, - const std::string& input) - { - if (input.size() > 0) - Compress(output, &input[0], input.size()); - else - Compress(output, NULL, 0); - } - - void BufferCompressor::Uncompress(std::string& output, - const std::string& input) - { - if (input.size() > 0) - Uncompress(output, &input[0], input.size()); - else - Uncompress(output, NULL, 0); - } -} diff -r 7962563129c9 -r 52dc56bcec7d Core/Compression/BufferCompressor.h --- a/Core/Compression/BufferCompressor.h Mon Aug 10 14:18:24 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -/** - * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics - * Department, University Hospital of Liege, Belgium - * - * This program is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * In addition, as a special exception, the copyright holders of this - * program give permission to link the code of its release with the - * OpenSSL project's "OpenSSL" library (or with modified versions of it - * that use the same license as the "OpenSSL" library), and distribute - * the linked executables. You must obey the GNU General Public License - * in all respects for all of the code used other than "OpenSSL". If you - * modify file(s) with this exception, you may extend this exception to - * your version of the file(s), but you are not obligated to do so. If - * you do not wish to do so, delete this exception statement from your - * version. If you delete this exception statement from all source files - * in the program, then also delete it here. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - **/ - - -#pragma once - -#include -#include -#include -#include - -namespace Orthanc -{ - class BufferCompressor - { - public: - virtual ~BufferCompressor() - { - } - - virtual void Compress(std::string& compressed, - const void* uncompressed, - size_t uncompressedSize) = 0; - - virtual void Uncompress(std::string& uncompressed, - const void* compressed, - size_t compressedSize) = 0; - - virtual void Compress(std::string& compressed, - const std::vector& uncompressed); - - virtual void Uncompress(std::string& uncompressed, - const std::vector& compressed); - - virtual void Compress(std::string& compressed, - const std::string& uncompressed); - - virtual void Uncompress(std::string& uncompressed, - const std::string& compressed); - }; -} diff -r 7962563129c9 -r 52dc56bcec7d Core/Compression/DeflateBaseCompressor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/Compression/DeflateBaseCompressor.cpp Mon Aug 10 14:52:10 2015 +0200 @@ -0,0 +1,49 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * In addition, as a special exception, the copyright holders of this + * program give permission to link the code of its release with the + * OpenSSL project's "OpenSSL" library (or with modified versions of it + * that use the same license as the "OpenSSL" library), and distribute + * the linked executables. You must obey the GNU General Public License + * in all respects for all of the code used other than "OpenSSL". If you + * modify file(s) with this exception, you may extend this exception to + * your version of the file(s), but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source files + * in the program, then also delete it here. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + **/ + + +#include "../PrecompiledHeaders.h" +#include "DeflateBaseCompressor.h" + +#include "../OrthancException.h" + +namespace Orthanc +{ + void DeflateBaseCompressor::SetCompressionLevel(uint8_t level) + { + if (level >= 10) + { + throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression"); + } + + compressionLevel_ = level; + } +} diff -r 7962563129c9 -r 52dc56bcec7d Core/Compression/DeflateBaseCompressor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/Compression/DeflateBaseCompressor.h Mon Aug 10 14:52:10 2015 +0200 @@ -0,0 +1,71 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * In addition, as a special exception, the copyright holders of this + * program give permission to link the code of its release with the + * OpenSSL project's "OpenSSL" library (or with modified versions of it + * that use the same license as the "OpenSSL" library), and distribute + * the linked executables. You must obey the GNU General Public License + * in all respects for all of the code used other than "OpenSSL". If you + * modify file(s) with this exception, you may extend this exception to + * your version of the file(s), but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source files + * in the program, then also delete it here. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + **/ + + +#pragma once + +#include "IBufferCompressor.h" + +#include + +namespace Orthanc +{ + class DeflateBaseCompressor : public IBufferCompressor + { + private: + uint8_t compressionLevel_; + bool prefixWithUncompressedSize_; + + public: + DeflateBaseCompressor() : + compressionLevel_(6), + prefixWithUncompressedSize_(false) + { + } + + void SetCompressionLevel(uint8_t level); + + void SetPrefixWithUncompressedSize(bool prefix) + { + prefixWithUncompressedSize_ = prefix; + } + + bool HasPrefixWithUncompressedSize() const + { + return prefixWithUncompressedSize_; + } + + uint8_t GetCompressionLevel() const + { + return compressionLevel_; + } + }; +} diff -r 7962563129c9 -r 52dc56bcec7d Core/Compression/IBufferCompressor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/Compression/IBufferCompressor.h Mon Aug 10 14:52:10 2015 +0200 @@ -0,0 +1,73 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * In addition, as a special exception, the copyright holders of this + * program give permission to link the code of its release with the + * OpenSSL project's "OpenSSL" library (or with modified versions of it + * that use the same license as the "OpenSSL" library), and distribute + * the linked executables. You must obey the GNU General Public License + * in all respects for all of the code used other than "OpenSSL". If you + * modify file(s) with this exception, you may extend this exception to + * your version of the file(s), but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source files + * in the program, then also delete it here. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + **/ + + +#pragma once + +#include +#include + +namespace Orthanc +{ + class IBufferCompressor : public boost::noncopyable + { + public: + virtual ~IBufferCompressor() + { + } + + virtual void Compress(std::string& compressed, + const void* uncompressed, + size_t uncompressedSize) = 0; + + virtual void Uncompress(std::string& uncompressed, + const void* compressed, + size_t compressedSize) = 0; + + static void Compress(std::string& compressed, + IBufferCompressor& compressor, + const std::string& uncompressed) + { + compressor.Compress(compressed, + uncompressed.size() == 0 ? NULL : uncompressed.c_str(), + uncompressed.size()); + } + + static void Uncompress(std::string& uncompressed, + IBufferCompressor& compressor, + const std::string& compressed) + { + compressor.Uncompress(uncompressed, + compressed.size() == 0 ? NULL : compressed.c_str(), + compressed.size()); + } + }; +} diff -r 7962563129c9 -r 52dc56bcec7d Core/Compression/ZlibCompressor.cpp --- a/Core/Compression/ZlibCompressor.cpp Mon Aug 10 14:18:24 2015 +0200 +++ b/Core/Compression/ZlibCompressor.cpp Mon Aug 10 14:52:10 2015 +0200 @@ -40,17 +40,6 @@ namespace Orthanc { - void ZlibCompressor::SetCompressionLevel(uint8_t level) - { - if (level >= 10) - { - throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression"); - } - - compressionLevel_ = level; - } - - void ZlibCompressor::Compress(std::string& compressed, const void* uncompressed, size_t uncompressedSize) @@ -68,7 +57,7 @@ } uint8_t* target; - if (prefixWithUncompressedSize_) + if (HasPrefixWithUncompressedSize()) { compressed.resize(compressedSize + sizeof(uint64_t)); target = reinterpret_cast(&compressed[0]) + sizeof(uint64_t); @@ -83,7 +72,7 @@ &compressedSize, const_cast(static_cast(uncompressed)), uncompressedSize, - compressionLevel_); + GetCompressionLevel()); if (error != Z_OK) { @@ -100,7 +89,7 @@ } // The compression was successful - if (prefixWithUncompressedSize_) + if (HasPrefixWithUncompressedSize()) { uint64_t s = static_cast(uncompressedSize); memcpy(&compressed[0], &s, sizeof(uint64_t)); diff -r 7962563129c9 -r 52dc56bcec7d Core/Compression/ZlibCompressor.h --- a/Core/Compression/ZlibCompressor.h Mon Aug 10 14:18:24 2015 +0200 +++ b/Core/Compression/ZlibCompressor.h Mon Aug 10 14:52:10 2015 +0200 @@ -32,41 +32,16 @@ #pragma once -#include "BufferCompressor.h" +#include "DeflateBaseCompressor.h" namespace Orthanc { - class ZlibCompressor : public BufferCompressor + class ZlibCompressor : public DeflateBaseCompressor { - private: - uint8_t compressionLevel_; - bool prefixWithUncompressedSize_; - public: - using BufferCompressor::Compress; - using BufferCompressor::Uncompress; - - ZlibCompressor() : - compressionLevel_(6), - prefixWithUncompressedSize_(true) + ZlibCompressor() { - } - - void SetCompressionLevel(uint8_t level); - - void SetPrefixWithUncompressedSize(bool prefix) - { - prefixWithUncompressedSize_ = prefix; - } - - bool HasPrefixWithUncompressedSize() const - { - return prefixWithUncompressedSize_; - } - - uint8_t GetCompressionLevel() const - { - return compressionLevel_; + SetPrefixWithUncompressedSize(true); } virtual void Compress(std::string& compressed, diff -r 7962563129c9 -r 52dc56bcec7d Core/FileStorage/CompressedFileStorageAccessor.cpp --- a/Core/FileStorage/CompressedFileStorageAccessor.cpp Mon Aug 10 14:18:24 2015 +0200 +++ b/Core/FileStorage/CompressedFileStorageAccessor.cpp Mon Aug 10 14:52:10 2015 +0200 @@ -135,7 +135,7 @@ { std::string compressed; GetStorageArea().Read(compressed, uuid, type); - zlib_.Uncompress(content, compressed); + IBufferCompressor::Uncompress(content, zlib_, compressed); break; } @@ -161,7 +161,7 @@ GetStorageArea().Read(compressed, uuid, type); std::auto_ptr sender(new BufferHttpSender); - zlib_.Uncompress(sender->GetBuffer(), compressed); + IBufferCompressor::Uncompress(sender->GetBuffer(), zlib_, compressed); return sender.release(); } diff -r 7962563129c9 -r 52dc56bcec7d UnitTestsSources/UnitTestsMain.cpp --- a/UnitTestsSources/UnitTestsMain.cpp Mon Aug 10 14:18:24 2015 +0200 +++ b/UnitTestsSources/UnitTestsMain.cpp Mon Aug 10 14:52:10 2015 +0200 @@ -98,14 +98,6 @@ ASSERT_FALSE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b_")); } -static void StringToVector(std::vector& v, - const std::string& s) -{ - v.resize(s.size()); - for (size_t i = 0; i < s.size(); i++) - v[i] = s[i]; -} - TEST(Zlib, Basic) { @@ -114,20 +106,10 @@ std::string compressed, compressed2; ZlibCompressor c; - c.Compress(compressed, s); - - std::vector v, vv; - StringToVector(v, s); - c.Compress(compressed2, v); - ASSERT_EQ(compressed, compressed2); + IBufferCompressor::Compress(compressed, c, s); std::string uncompressed; - c.Uncompress(uncompressed, compressed); - ASSERT_EQ(s.size(), uncompressed.size()); - ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); - - StringToVector(vv, compressed); - c.Uncompress(uncompressed, vv); + IBufferCompressor::Uncompress(uncompressed, c, compressed); ASSERT_EQ(s.size(), uncompressed.size()); ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); } @@ -141,10 +123,10 @@ std::string compressed, compressed2; ZlibCompressor c; c.SetCompressionLevel(9); - c.Compress(compressed, s); + IBufferCompressor::Compress(compressed, c, s); c.SetCompressionLevel(0); - c.Compress(compressed2, s); + IBufferCompressor::Compress(compressed2, c, s); ASSERT_TRUE(compressed.size() < compressed2.size()); } @@ -157,32 +139,26 @@ std::string compressed; ZlibCompressor c; - c.Compress(compressed, s); + IBufferCompressor::Compress(compressed, c, s); compressed[compressed.size() - 1] = 'a'; std::string u; - ASSERT_THROW(c.Uncompress(u, compressed), OrthancException); + ASSERT_THROW(IBufferCompressor::Uncompress(u, c, compressed), OrthancException); } TEST(Zlib, Empty) { std::string s = ""; - std::vector v, vv; std::string compressed, compressed2; ZlibCompressor c; - c.Compress(compressed, s); - c.Compress(compressed2, v); + IBufferCompressor::Compress(compressed, c, s); ASSERT_EQ(compressed, compressed2); std::string uncompressed; - c.Uncompress(uncompressed, compressed); - ASSERT_EQ(0u, uncompressed.size()); - - StringToVector(vv, compressed); - c.Uncompress(uncompressed, vv); + IBufferCompressor::Uncompress(uncompressed, c, compressed); ASSERT_EQ(0u, uncompressed.size()); }