libfilezilla
encode.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_ENCODE_HEADER
2 #define LIBFILEZILLA_ENCODE_HEADER
3 
4 #include "libfilezilla.hpp"
5 
6 #include <string>
7 #include <vector>
8 
15 namespace fz {
16 
23 template<typename Char>
24 int hex_char_to_int(Char c)
25 {
26  if (c >= 'a' && c <= 'f') {
27  return c - 'a' + 10;
28  }
29  if (c >= 'A' && c <= 'F') {
30  return c - 'A' + 10;
31  }
32  else if (c >= '0' && c <= '9') {
33  return c - '0';
34  }
35  return -1;
36 }
37 
39 template<typename OutString, typename String>
40 OutString hex_decode_impl(String const& in)
41 {
42  OutString ret;
43  if (!(in.size() % 2)) {
44  ret.reserve(in.size() / 2);
45  for (size_t i = 0; i < in.size(); i += 2) {
46  int high = hex_char_to_int(in[i]);
47  int low = hex_char_to_int(in[i + 1]);
48  if (high == -1 || low == -1) {
49  return OutString();
50  }
51  ret.push_back(static_cast<typename OutString::value_type>((high << 4) + low));
52  }
53  }
54 
55  return ret;
56 }
57 
58 template<typename OutString = std::vector<uint8_t>>
59 OutString hex_decode(std::string_view const& in)
60 {
61  return hex_decode_impl<OutString>(in);
62 }
63 
64 template<typename OutString = std::vector<uint8_t>>
65 OutString hex_decode(std::wstring_view const& in)
66 {
67  return hex_decode_impl<OutString>(in);
68 }
69 
76 template<typename Char = char, bool Lowercase = true>
77 Char int_to_hex_char(int d)
78 {
79  if (d > 9) {
80  return static_cast<Char>((Lowercase ? 'a' : 'A') + d - 10);
81  }
82  else {
83  return static_cast<Char>('0' + d);
84  }
85 }
86 
87 template<typename String, typename InString, bool Lowercase = true>
88 String hex_encode(InString const& data)
89 {
90  static_assert(sizeof(typename InString::value_type) == 1, "Input must be a container of 8 bit values");
91  String ret;
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));
96  }
97 
98  return ret;
99 }
100 
107 enum class base64_type {
108  standard,
109  url
110 };
111 
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);
115 
121 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::string_view const& in);
122 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::wstring_view const& in);
123 std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::string_view const& in);
124 std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::wstring_view const& in);
125 
126 
133 enum class base32_type {
134  standard,
135  base32hex,
136  locale_safe
137 };
138 
140 std::string FZ_PUBLIC_SYMBOL base32_encode(std::string_view const& in, base32_type type = base32_type::standard, bool pad = true);
141 std::string FZ_PUBLIC_SYMBOL base32_encode(std::vector<uint8_t> const& in, base32_type type = base32_type::standard, bool pad = true);
142 
148 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::string_view const& in, base32_type type = base32_type::standard);
149 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::wstring_view const& in, base32_type type = base32_type::standard);
150 std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::string_view const& in, base32_type type = base32_type::standard);
151 std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::wstring_view const& in, base32_type type = base32_type::standard);
152 
161 std::string FZ_PUBLIC_SYMBOL percent_encode(std::string_view const& s, bool keep_slashes = false);
162 std::string FZ_PUBLIC_SYMBOL percent_encode(std::wstring_view const& s, bool keep_slashes = false);
163 
169 std::wstring FZ_PUBLIC_SYMBOL percent_encode_w(std::wstring_view const& s, bool keep_slashes = false);
170 
176 std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::string_view const& s, bool allow_embedded_null = false);
177 std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::wstring_view const& s, bool allow_embedded_null = false);
178 std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::string_view const& s, bool allow_embedded_null = false);
179 std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::wstring_view const& s, bool allow_embedded_null = false);
180 
181 }
182 
183 #endif
std::string base64_encode(std::string_view const &in, base64_type type=base64_type::standard, bool pad=true)
Encodes raw input string to base64.
base32_type
Alphabet variations for base32.
Definition: encode.hpp:133
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::vector< uint8_t > base32_decode(std::string_view const &in, base32_type type=base32_type::standard)
Decodes base32, ignores whitespace. Returns empty string on invalid input.
std::string percent_encode(std::string_view const &s, bool keep_slashes=false)
Percent-encodes string.
std::vector< uint8_t > 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:17
std::string base32_encode(std::string_view const &in, base32_type type=base32_type::standard, bool pad=true)
Encodes raw input string to base32.
std::vector< uint8_t > 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-encodes wide-character. Non-ASCII characters are converted to UTF-8 before they are encoded.