1 /**
2  * Orthanc - A Lightweight, RESTful DICOM Store
3  * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4  * Department, University Hospital of Liege, Belgium
5  * Copyright (C) 2017-2020 Osimis S.A., Belgium
6  *
7  * This program is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program. If not, see
19  * <http://www.gnu.org/licenses/>.
20  **/
21 
22 
23 #include "../PrecompiledHeaders.h"
24 #include "DeflateBaseCompressor.h"
25 
26 #include "../OrthancException.h"
27 #include "../Logging.h"
28 
29 #include <string.h>
30 
31 namespace Orthanc
32 {
DeflateBaseCompressor()33   DeflateBaseCompressor::DeflateBaseCompressor() :
34     compressionLevel_(6),
35     prefixWithUncompressedSize_(false)
36   {
37   }
38 
39 
SetCompressionLevel(uint8_t level)40   void DeflateBaseCompressor::SetCompressionLevel(uint8_t level)
41   {
42     if (level >= 10)
43     {
44       throw OrthancException(ErrorCode_ParameterOutOfRange,
45                              "Zlib compression level must be between 0 (no compression) and 9 (highest compression)");
46     }
47 
48     compressionLevel_ = level;
49   }
50 
51 
ReadUncompressedSizePrefix(const void * compressed,size_t compressedSize)52   uint64_t DeflateBaseCompressor::ReadUncompressedSizePrefix(const void* compressed,
53                                                              size_t compressedSize)
54   {
55     if (compressedSize == 0)
56     {
57       return 0;
58     }
59 
60     if (compressedSize < sizeof(uint64_t))
61     {
62       throw OrthancException(ErrorCode_CorruptedFile, "The compressed buffer is ill-formed");
63     }
64 
65     uint64_t size;
66     memcpy(&size, compressed, sizeof(uint64_t));
67 
68     return size;
69   }
70 
71 
SetPrefixWithUncompressedSize(bool prefix)72   void DeflateBaseCompressor::SetPrefixWithUncompressedSize(bool prefix)
73   {
74     prefixWithUncompressedSize_ = prefix;
75   }
76 
HasPrefixWithUncompressedSize() const77   bool DeflateBaseCompressor::HasPrefixWithUncompressedSize() const
78   {
79     return prefixWithUncompressedSize_;
80   }
81 
GetCompressionLevel() const82   uint8_t DeflateBaseCompressor::GetCompressionLevel() const
83   {
84     return compressionLevel_;
85   }
86 }
87