comparison Core/Compression/ZlibCompressor.cpp @ 1512:52dc56bcec7d

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 10 Aug 2015 14:52:10 +0200
parents 7962563129c9
children fe07f82d83d3
comparison
equal deleted inserted replaced
1511:7962563129c9 1512:52dc56bcec7d
38 #include <zlib.h> 38 #include <zlib.h>
39 #include "../OrthancException.h" 39 #include "../OrthancException.h"
40 40
41 namespace Orthanc 41 namespace Orthanc
42 { 42 {
43 void ZlibCompressor::SetCompressionLevel(uint8_t level)
44 {
45 if (level >= 10)
46 {
47 throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression");
48 }
49
50 compressionLevel_ = level;
51 }
52
53
54 void ZlibCompressor::Compress(std::string& compressed, 43 void ZlibCompressor::Compress(std::string& compressed,
55 const void* uncompressed, 44 const void* uncompressed,
56 size_t uncompressedSize) 45 size_t uncompressedSize)
57 { 46 {
58 if (uncompressedSize == 0) 47 if (uncompressedSize == 0)
66 { 55 {
67 compressedSize = 1; 56 compressedSize = 1;
68 } 57 }
69 58
70 uint8_t* target; 59 uint8_t* target;
71 if (prefixWithUncompressedSize_) 60 if (HasPrefixWithUncompressedSize())
72 { 61 {
73 compressed.resize(compressedSize + sizeof(uint64_t)); 62 compressed.resize(compressedSize + sizeof(uint64_t));
74 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t); 63 target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
75 } 64 }
76 else 65 else
81 70
82 int error = compress2(target, 71 int error = compress2(target,
83 &compressedSize, 72 &compressedSize,
84 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), 73 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)),
85 uncompressedSize, 74 uncompressedSize,
86 compressionLevel_); 75 GetCompressionLevel());
87 76
88 if (error != Z_OK) 77 if (error != Z_OK)
89 { 78 {
90 compressed.clear(); 79 compressed.clear();
91 80
98 throw OrthancException(ErrorCode_InternalError); 87 throw OrthancException(ErrorCode_InternalError);
99 } 88 }
100 } 89 }
101 90
102 // The compression was successful 91 // The compression was successful
103 if (prefixWithUncompressedSize_) 92 if (HasPrefixWithUncompressedSize())
104 { 93 {
105 uint64_t s = static_cast<uint64_t>(uncompressedSize); 94 uint64_t s = static_cast<uint64_t>(uncompressedSize);
106 memcpy(&compressed[0], &s, sizeof(uint64_t)); 95 memcpy(&compressed[0], &s, sizeof(uint64_t));
107 compressed.resize(compressedSize + sizeof(uint64_t)); 96 compressed.resize(compressedSize + sizeof(uint64_t));
108 } 97 }