BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnFileReaderZLibNG.cxx
Go to the documentation of this file.
1// headers from this target
3
4// std:: headers
5#include <cassert>
6#include <filesystem>
7// the main ZLib header
8#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
9#include <zlib-ng.h>
10#endif // BMNCOMPRESSION_WITH_ZLIB_NG
11
12// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
13// ================== BmnFileReaderZLibNG public methods ==================
14// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
15
16// ========== BmnIFileReaderInterface ==========
17
18BmnFileReaderZLibNG::BmnFileReaderZLibNG([[maybe_unused]] const std::filesystem::path& aFilePath)
20 , fZlibNgStream(nullptr)
21 , fArchiveReadManager(*this)
22 , fNumberOfReadBytes(0ull)
24{
25#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
26 if (aFilePath.empty())
27 return;
28 Open(aFilePath);
29#endif // BMNCOMPRESSION_WITH_ZLIB_NG
30}
31
33{
35 // another delete here as Close might go bad and still not break the code (in Release builds)
36#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
37 delete fZlibNgStream;
38#endif // BMNCOMPRESSION_WITH_ZLIB_NG
39}
40
41void BmnFileReaderZLibNG::Open([[maybe_unused]] const std::filesystem::path& aFilePath)
42{
43#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
44 /*
45 While we actually don't need so many comments and checks here, we are still performing them just to ensure
46 one doesn't make an accidental mistake - it might be easier to catch it
47 */
48 if (fZlibNgStream) {
49 LOG(error) << "ZLib-ng stream is already valid!";
50 return;
51 }
53 return;
54 fArchiveReadManager.Open(aFilePath, std::ios::in | std::ios::binary);
56 new zng_stream_s{nullptr, 0u, 0u, nullptr, 0u, 0u, nullptr, nullptr, nullptr, nullptr, nullptr, 0, 0u, 0u};
57 // according to documentation, adding +32 enables auto-detection of headers, i.e., the stream will decide whether to
58 // use gzip or zlib formats
59 if (zng_inflateInit2(fZlibNgStream, 15 + 32) != Z_OK) {
60 LOG(error) << "Failed to initialize ZLib-ng stream! Reason: " << fZlibNgStream->msg << '\n';
61 zng_inflateEnd(fZlibNgStream);
62 delete fZlibNgStream;
63 fZlibNgStream = nullptr;
64 fArchiveReadManager.Close();
65 }
66
67#else
68 throw std::runtime_error{"Trying to use ZLIB-NG while BMN is not compiled with it!"};
69#endif // BMNCOMPRESSION_WITH_ZLIB_NG
70}
71
73{
74#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
75 if (!fZlibNgStream)
76 return;
77 if (zng_inflateEnd(fZlibNgStream) != Z_OK) {
78 LOG(fatal) << "Failed to free ZLib-ng stream - possible huge memory leak! Failure reason: "
79 << fZlibNgStream->msg << '\n';
80 std::terminate();
81 }
82 delete fZlibNgStream;
83 fZlibNgStream = nullptr;
84 fArchiveReadManager.Close();
85 fNumberOfReadBytes = 0ull;
87#endif // BMNCOMPRESSION_WITH_ZLIB_NG
88}
89
90int64_t BmnFileReaderZLibNG::Read([[maybe_unused]] void* dataToFill, [[maybe_unused]] const int64_t sizeToFill)
91{
92#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
94 return 0ll;
95 return fArchiveReadManager.Read(dataToFill, sizeToFill);
96#else
97 throw std::runtime_error{"Trying to use ZLIB-NG while BMN is not compiled with it!"};
98#endif // BMNCOMPRESSION_WITH_ZLIB_NG
99}
100
101int64_t BmnFileReaderZLibNG::Tell() const noexcept
102{
103 return (int64_t)(fNumberOfReadBytes + fArchiveReadManager.GetCurrentOutputBufferPosition()
104 - fArchiveReadManager.GetTotalOutputBufferFilledSize());
105}
106
107void BmnFileReaderZLibNG::Seek([[maybe_unused]] int64_t value)
108{
109#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
110 if (value < 0ll || value == Tell())
111 return;
112 if (!IsStreamInGoodState()) {
113 LOG(error) << "Before calling `BmnFileReaderZLibNG::Seek` ensure to clear bad flags!\n";
114 return;
115 }
116 if (!fZlibNgStream) {
117 LOG(error) << "Before calling `BmnFileReaderZLibNG::Seek` ensure to open the stream!\n";
118 return;
119 }
120 if (!fArchiveReadManager.IsOpen()) {
121 LOG(error) << "Reading file missing while calling `BmnFileReaderZLibNG::Seek`\n";
122 return;
123 }
124 if ((size_t)value < (fNumberOfReadBytes - fArchiveReadManager.GetTotalOutputBufferFilledSize())) {
125 if (zng_inflateReset(fZlibNgStream) != Z_OK) {
126 LOG(error) << "Failed to reinitialize ZLib-ng stream! Removing corrupted data...";
127 Close();
128 return;
129 }
130 fNumberOfReadBytes = 0ull;
131 fArchiveReadManager.Seek(0ll);
132 }
133 // in theory I could check IsOpen again for unexpected errors, but....
134 fArchiveReadManager.MoveToDecompressingLocation(value);
135#else
136 throw std::runtime_error{"Trying to use ZLIB-NG while BMN is not compiled with it!"};
137#endif // BMNCOMPRESSION_WITH_ZLIB_NG
138}
139
140bool BmnFileReaderZLibNG::IsGood() const noexcept
141{
142#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
144#else
145 return false;
146#endif // BMNCOMPRESSION_WITH_ZLIB_NG
147}
148
149bool BmnFileReaderZLibNG::IsEOF() const noexcept
150{
151 return fArchiveReadManager.IsEOF();
152}
153
155{
156 // I don't like this method much
158 fArchiveReadManager.Clear();
159}
160
161bool BmnFileReaderZLibNG::IsOpen() const noexcept
162{
163 return fArchiveReadManager.IsOpen() && fZlibNgStream;
164}
165
170
171// ========== BmnIArchiveLib Interface ==========
172
173BmnFileStreamResults BmnFileReaderZLibNG::Uncompress([[maybe_unused]] const void* source,
174 [[maybe_unused]] const size_t sourceTotalSize,
175 [[maybe_unused]] size_t& numberOfReadBytes,
176 [[maybe_unused]] void* output,
177 [[maybe_unused]] const size_t outputTotalSize,
178 [[maybe_unused]] size_t& numberOfSetOutputBytes)
179{
180 if (!fZlibNgStream)
182#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
183 if (!IsStreamInGoodState())
185 fZlibNgStream->next_out = (Bytef*)output;
186 fZlibNgStream->avail_out = (uInt)outputTotalSize;
187 fZlibNgStream->total_out = 0u;
188 fZlibNgStream->next_in = (Bytef*)source;
189 fZlibNgStream->avail_in = (uInt)sourceTotalSize;
190 fZlibNgStream->total_in = 0u;
191 fLastReturnStatus = zng_inflate(fZlibNgStream, Z_NO_FLUSH);
192 if (!IsStreamInGoodState())
194 numberOfReadBytes = (size_t)fZlibNgStream->total_in;
195 numberOfSetOutputBytes = (size_t)fZlibNgStream->total_out;
196 fNumberOfReadBytes += numberOfSetOutputBytes;
197 if (fLastReturnStatus == Z_STREAM_END)
200#else
201 throw std::runtime_error{"Trying to use ZLIB-NG while BMN is not compiled with it!"};
202#endif // BMNCOMPRESSION_WITH_ZLIB_NG
203}
204
206{
207#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
208 std::string strToReturn{};
209 if (fZlibNgStream && fZlibNgStream->msg)
210 strToReturn = std::string{fZlibNgStream->msg};
211 return strToReturn;
212#else
213 return std::string{"BMN NOT BUILT WITH ZLIB"};
214#endif // BMNCOMPRESSION_WITH_ZLIB_NG
215}
216
218{
219 return fNumberOfReadBytes;
220}
221
223{
224#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
225 if (!IsStreamInGoodState())
226 return true;
227 return fLastReturnStatus == Z_STREAM_END;
228#else
229 return true;
230#endif // BMNCOMPRESSION_WITH_ZLIB_NG
231}
232
234{
235#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
236 return fLastReturnStatus >= 0;
237#else
238 return false;
239#endif // BMNCOMPRESSION_WITH_ZLIB_NG
240}
virtual int64_t Read(void *dataToFill, const int64_t sizeToFill) override
virtual bool IsStreamInGoodState() const noexcept override
virtual void Close() noexcept override
virtual bool IsGood() const noexcept override
virtual int64_t Tell() const noexcept override
virtual BmnFileType GetFileType() const noexcept override
virtual std::string GetLastUncompressErrorDescription() const override
virtual bool HasReachedTheEndOfStream() const noexcept override
virtual BmnFileStreamResults Uncompress(const void *source, const size_t sourceTotalSize, size_t &numberOfReadBytes, void *output, const size_t outputTotalSize, size_t &numberOfSetOutputBytes) override
virtual void ClearStateFlags() override
virtual ~BmnFileReaderZLibNG() override
virtual bool IsOpen() const noexcept override
virtual size_t GetNumberOfDecompressedBytes() const noexcept override
virtual void Open(const std::filesystem::path &aFilePath) override
BmnFileReaderZLibNG(const std::filesystem::path &aFilePath="")
virtual bool IsEOF() const noexcept override
BmnReaderCacheManager< 32ull *1024ull, 64ull *1024ull > fArchiveReadManager
virtual void Seek(int64_t value) override
BmnIFileReader()=default
bool PerformStandardChecksBeforeOpening(const std::filesystem::path &aFilePath) const noexcept