BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnCompressionGlobalFunctions.cxx
Go to the documentation of this file.
1// headers from this target
3
5#include "BmnFileReaderRaw.h"
7#include "BmnFileReaderZLib.h"
9#include "BmnFileReaderZStd.h"
10
11// std:: headers
12#include <algorithm>
13#include <array>
14#include <cctype>
15#include <iterator>
16#include <string>
17
18#ifdef BMNCOMPRESSION_WITH_ZSTD
19#include <zstd.h>
20constexpr uint32_t co_zstdMagicNumber = ZSTD_MAGICNUMBER;
21#else
22constexpr uint32_t co_zstdMagicNumber = 0xFD2FB528;
23#endif // BMNCOMPRESSION_WITH_ZSTD
24
25// 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
26// ================== static function declaration ==================
27// 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
28
29/*
30 * A translation-unit function that will be updated as we add new reading libraries. This function
31 * opens a stream for the provided path (if one exists) and based on magic bytes, it returns what kind
32 * of file reader should be created
33 * @param aFilePath A path to an existing actual and accessible file that should be read
34 * @param aUserSelectedAlgorithm A preferred algorithm selected by a user and passed into
35 * `BmnCompressionIsArchiveLibAvailable`
36 * @return A type of the file that should be opened, or `BmnFileType::Unknown` if the library of the current file is not
37 * supported (compiled)
38 */
39[[nodiscard]] static BmnFileType DoCommonProcessingOfFileFinding(const std::filesystem::path& aFilePath,
40 const BmnFileType aUserSelectedAlgorithm);
41
42// 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
43// ================== Global functions ==================
44// 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
45
46bool BmnCompressionIsArchiveLibAvailable(const BmnFileType aLibraryType) noexcept
47{
48 switch (aLibraryType) {
50#ifdef BMNCOMPRESSION_WITH_ZSTD
51 return true;
52#else
53 return false;
54#endif // BMNCOMPRESSION_WITH_ZSTD
56#ifdef BMNCOMPRESSION_WITH_ZLIB
57 return true;
58#else
59 return false;
60#endif // BMNCOMPRESSION_WITH_ZLIB
62#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
63 return true;
64#else
65 return false;
66#endif // BMNCOMPRESSION_WITH_ZLIB_NG
68#ifdef BMNCOMPRESSION_WITH_XZ_LZMA
69 return true;
70#else
71 return false;
72#endif // BMNCOMPRESSION_WITH_XZ_LZMA
74#ifdef BMNCOMPRESSION_WITH_BZIP3
75 return true;
76#else
77 return false;
78#endif // BMNCOMPRESSION_WITH_BZIP3
80 return true;
82 [[fallthrough]];
83 default:
84 return false;
85 }
86}
87
88BmnIFileReader* BmnCompressionCreateNewSuitableReaderForAFile(const std::filesystem::path& aFilePath,
89 const BmnFileType aPreferredTypeForThisAlgorithm)
90{
91 BmnFileType aNeededType = DoCommonProcessingOfFileFinding(aFilePath, aPreferredTypeForThisAlgorithm);
92 switch (aNeededType) {
93#ifdef BMNCOMPRESSION_WITH_ZSTD
95 return new BmnFileReaderZStd{aFilePath};
96#endif // BMNCOMPRESSION_WITH_ZSTD
97#ifdef BMNCOMPRESSION_WITH_ZLIB
99 return new BmnFileReaderZLib{aFilePath};
100#endif // BMNCOMPRESSION_WITH_ZLIB
101#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
103 return new BmnFileReaderZLibNG{aFilePath};
104#endif // BMNCOMPRESSION_WITH_ZLIB_NG
105#ifdef BMNCOMPRESSION_WITH_XZ_LZMA
107 return new BmnFileReaderXzLzma{aFilePath};
108#endif // BMNCOMPRESSION_WITH_XZ_LZMA
109#ifdef BMNCOMPRESSION_WITH_BZIP3
111 return new BmnFileReaderBZip3{aFilePath};
112#endif // BMNCOMPRESSION_WITH_BZIP3
113 case BmnFileType::Raw:
114 return new BmnFileReaderRaw{aFilePath};
116 [[fallthrough]];
117 default:
118 return nullptr;
119 }
120}
121
122std::filesystem::path BmnCompressionRemoveArchiveExtension(std::filesystem::path aFilePath)
123{
124 std::string fileExtension = aFilePath.extension().string();
125 // '.ZST' names the very same format as '.zst', so the extensions are compared in the lower case
126 std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(),
127 [](unsigned char aSymbol) { return static_cast<char>(std::tolower(aSymbol)); });
128
129 // the list of the archive extensions is declared in BmnCompressionGlobalFunctions.h
130 if (std::find(std::cbegin(BmnCompressionArchiveExtensions), std::cend(BmnCompressionArchiveExtensions),
131 fileExtension)
133 return aFilePath;
134
135 return aFilePath.replace_extension();
136}
137
138// 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
139// ================== static function definition ==================
140// 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
141
142static BmnFileType DoCommonProcessingOfFileFinding(const std::filesystem::path& aFilePath,
143 [[maybe_unused]] const BmnFileType aUserSelectedAlgorithm)
144{
145 if (aFilePath.empty()) {
146 LOG(error) << "You did not provide a path for opening a file reader!\n";
148 }
149 try {
150 if (!std::filesystem::exists(aFilePath) || !std::filesystem::is_regular_file(aFilePath)) {
151 LOG(error) << "You should provide an existing path to an actual file for opening a file reader!\n";
153 }
154 } catch (const std::bad_alloc& errorDescription) {
155 LOG(error) << "Filesystem failure! Reason: " << errorDescription.what() << '\n';
157 } catch (const std::filesystem::filesystem_error& errorDescription) {
158 LOG(error) << "Filesystem failure! Reason: " << errorDescription.what() << '\n';
160 }
161 std::ifstream inStream{};
162 inStream.open(aFilePath, std::ios::in | std::ios::binary);
163 if (!inStream.is_open()) {
164 LOG(error) << "Failed to open a file for checking a magic byte for opening a file reader!\n";
166 }
167 std::array<char, 15> beginningOfTheStream{};
168 inStream.read(beginningOfTheStream.data(), beginningOfTheStream.size());
169 // ================================================================================================================
170 if (*reinterpret_cast<uint32_t*>(beginningOfTheStream.data()) == co_zstdMagicNumber) {
171#ifdef BMNCOMPRESSION_WITH_ZSTD
172 return BmnFileType::ZStd;
173#else // !BMNCOMPRESSION_WITH_ZSTD
174 LOG(warning) << "Trying to open a ZStd file while BM@N root was not compiled to support the format - failed to "
175 "open a stream!\n";
177#endif // BMNCOMPRESSION_WITH_ZSTD
178 }
179 // ================================================================================================================
180 // I don't see this information in the XZ_LZMA API. Still, I found it on the internet and seems to work
181 constexpr char xzMagicByte[7] = {"\xFD\x37\x7A\x58\x5A\x00"};
182 if (std::memcmp((void*)beginningOfTheStream.data(), (void*)xzMagicByte, 6ull) == 0) {
183#ifdef BMNCOMPRESSION_WITH_XZ_LZMA
185#else // !BMNCOMPRESSION_WITH_XZ_LZMA
186 LOG(warning) << "Trying to open a XZ-LZMA file while BM@N root was not compiled to support the format - failed "
187 "to open a stream!\n";
189#endif // BMNCOMPRESSION_WITH_XZ_LZMA
190 }
191 // ================================================================================================================
192 // for now we expect GZip compression while later we might add additional support for files compressed directly by
193 // us. the deflate format itself doesn't have a magic byte, while zlib has various headers, but we will not be
194 // writing a direct ZLIB stream anyway
195 if (beginningOfTheStream[0] == '\x1F' && beginningOfTheStream[1] == '\x8B') {
196#if defined(BMNCOMPRESSION_WITH_ZLIB) || defined(BMNCOMPRESSION_WITH_ZLIB_NG)
197#ifdef BMNCOMPRESSION_WITH_ZLIB_NG
198#ifdef BMNCOMPRESSION_WITH_ZLIB
199 if (aUserSelectedAlgorithm == BmnFileType::ZLib)
200 return BmnFileType::ZLib;
201#endif // BMNCOMPRESSION_WITH_ZLIB
203#else // defined(BMNCOMPRESSION_WITH_ZLIB) && !defined(BMNCOMPRESSION_WITH_ZLIB_NG)
204 return BmnFileType::ZLib;
205#endif // BMNCOMPRESSION_WITH_ZLIB_NG
206#else // !defined(BMNCOMPRESSION_WITH_ZLIB) && !defined(BMNCOMPRESSION_WITH_ZLIB_NG)
207 LOG(warning) << "Trying to open a ZLlib/ZLib_ng file while BM@N root was not compiled to support the format - "
208 "failed to open a stream!\n";
210#endif // defined(BMNCOMPRESSION_WITH_ZLIB) || defined(BMNCOMPRESSION_WITH_ZLIB_NG)
211 }
212 // ================================================================================================================
213 if (std::memcmp((void*)beginningOfTheStream.data(), (void*)"BZ3v1", 5ull) == 0) {
214#ifdef BMNCOMPRESSION_WITH_BZIP3
215 return BmnFileType::BZip3;
216#else // !BMNCOMPRESSION_WITH_BZIP3
217 LOG(warning) << "Trying to open a BZip3 file while BM@N root was not compiled to support the format - failed "
218 "to open a stream!\n";
220#endif // BMNCOMPRESSION_WITH_BZIP3
221 }
222 // ================================================================================================================
223 return BmnFileType::Raw;
224}
bool BmnCompressionIsArchiveLibAvailable(const BmnFileType aLibraryType) noexcept
constexpr uint32_t co_zstdMagicNumber
std::filesystem::path BmnCompressionRemoveArchiveExtension(std::filesystem::path aFilePath)
BmnIFileReader * BmnCompressionCreateNewSuitableReaderForAFile(const std::filesystem::path &aFilePath, const BmnFileType aPreferredTypeForThisAlgorithm)
static BmnFileType DoCommonProcessingOfFileFinding(const std::filesystem::path &aFilePath, const BmnFileType aUserSelectedAlgorithm)
constexpr std::string_view BmnCompressionArchiveExtensions[]