1 #ifndef LIBFILEZILLA_ENCODE_HEADER
2 #define LIBFILEZILLA_ENCODE_HEADER
23 template<
typename Char>
26 if (c >=
'a' && c <=
'z') {
29 if (c >=
'A' && c <=
'Z') {
32 else if (c >=
'0' && c <=
'9') {
39 template<
typename OutString,
typename String>
40 OutString hex_decode_impl(String
const& in)
43 if (!(in.size() % 2)) {
44 ret.reserve(in.size() / 2);
45 for (
size_t i = 0; i < in.size(); i += 2) {
48 if (high == -1 || low == -1) {
51 ret.push_back(static_cast<typename OutString::value_type>((high << 4) + low));
58 template<
typename OutString = std::vector<u
int8_t>>
59 OutString hex_decode(std::string_view
const& in)
61 return hex_decode_impl<OutString>(in);
64 template<
typename OutString = std::vector<u
int8_t>>
65 OutString hex_decode(std::wstring_view
const& in)
67 return hex_decode_impl<OutString>(in);
76 template<
typename Char =
char,
bool Lowercase = true>
80 return static_cast<Char
>((Lowercase ?
'a' :
'A') + d - 10);
83 return static_cast<Char
>(
'0' + d);
87 template<
typename String,
typename InString,
bool Lowercase = true>
88 String hex_encode(InString
const& data)
90 static_assert(
sizeof(
typename InString::value_type) == 1,
"Input must be a container of 8 bit values");
92 ret.reserve(data.size() * 2);
93 for (
auto const& c : data) {
94 ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) >> 4));
95 ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) & 0xf));
113 std::string FZ_PUBLIC_SYMBOL
base64_encode(std::string_view
const& in,
base64_type type = base64_type::standard,
bool pad =
true);
114 std::string FZ_PUBLIC_SYMBOL
base64_encode(std::vector<uint8_t>
const& in,
base64_type type = base64_type::standard,
bool pad =
true);
121 std::string FZ_PUBLIC_SYMBOL
base64_decode(std::string_view
const& in);
132 std::string FZ_PUBLIC_SYMBOL
percent_encode(std::string_view
const& s,
bool keep_slashes =
false);
133 std::string FZ_PUBLIC_SYMBOL
percent_encode(std::wstring_view
const& s,
bool keep_slashes =
false);
140 std::wstring FZ_PUBLIC_SYMBOL
percent_encode_w(std::wstring_view
const& s,
bool keep_slashes =
false);
147 std::string FZ_PUBLIC_SYMBOL
percent_decode(std::string_view
const& s,
bool allow_embedded_null =
false);
std::string base64_encode(std::string_view const &in, base64_type type=base64_type::standard, bool pad=true)
Encodes raw input string to base64.
Char int_to_hex_char(int d)
Converts an integer to the corresponding lowercase hex digit.
Definition: encode.hpp:77
base64_type
Alphabet variations for base64.
Definition: encode.hpp:107
int hex_char_to_int(Char c)
Converts a hex digit to decimal int.
Definition: encode.hpp:24
std::string percent_encode(std::string_view const &s, bool keep_slashes=false)
Percent-enodes string.
std::string base64_decode(std::string_view const &in)
Decodes base64, ignores whitespace. Returns empty string on invalid input.
The namespace used by libfilezilla.
Definition: apply.hpp:16
std::string percent_decode(std::string_view const &s, bool allow_embedded_null=false)
Percent-decodes string.
Sets some global macros and further includes string.hpp.
std::wstring percent_encode_w(std::wstring_view const &s, bool keep_slashes=false)
Percent-enodes wide-character. Non-ASCII characters are converted to UTF-8 befor they are encoded...