6 #ifndef TILESON_BASE64DECOMPRESSOR_HPP
7 #define TILESON_BASE64DECOMPRESSOR_HPP
9 #include "../interfaces/IDecompressor.hpp"
17 [[nodiscard]]
inline const std::string &
name()
const override;
19 inline std::string
decompress(
const std::string_view &s)
override;
22 inline std::string
decompress(
const void *data,
size_t size)
override;
25 inline unsigned int pos_of_char(
const unsigned char chr);
26 inline static const std::string NAME =
"base64";
37 size_t length_of_string = s.length();
38 if (!length_of_string)
return std::string(
"");
40 size_t in_len = length_of_string;
49 size_t approx_length_of_decoded_string = length_of_string / 4 * 3;
51 ret.reserve(approx_length_of_decoded_string);
53 while (pos < in_len) {
55 unsigned int pos_of_char_1 = pos_of_char(s[pos+1] );
57 ret.push_back(
static_cast<std::string::value_type
>( ( (pos_of_char(s[pos+0]) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4)));
59 if (s[pos+2] !=
'=' && s[pos+2] !=
'.') {
61 unsigned int pos_of_char_2 = pos_of_char(s[pos+2] );
62 ret.push_back(
static_cast<std::string::value_type
>( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2)));
64 if (s[pos+3] !=
'=' && s[pos+3] !=
'.') {
65 ret.push_back(
static_cast<std::string::value_type
>( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(s[pos+3]) ));
75 unsigned int Base64Decompressor::pos_of_char(
const unsigned char chr)
81 if (chr >=
'A' && chr <=
'Z')
return chr -
'A';
82 else if (chr >=
'a' && chr <=
'z')
return chr -
'a' + (
'Z' -
'A') + 1;
83 else if (chr >=
'0' && chr <=
'9')
return chr -
'0' + (
'Z' -
'A') + (
'z' -
'a') + 2;
84 else if (chr ==
'+' || chr ==
'-')
return 62;
85 else if (chr ==
'/' || chr ==
'_')
return 63;
87 throw "If input is correct, this line should never be reached.";
107 return std::string();
Definition: Base64Decompressor.hpp:15
std::string decompressFile(const fs::path &path) override
Definition: Base64Decompressor.hpp:95
std::string decompress(const std::string_view &s) override
Definition: Base64Decompressor.hpp:34
const std::string & name() const override
Definition: Base64Decompressor.hpp:29
Definition: IDecompressor.hpp:14
Definition: Base64.hpp:12