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 class buffer;
17 
24 template<typename Char>
25 int hex_char_to_int(Char c)
26 {
27  if (c >= 'a' && c <= 'f') {
28  return c - 'a' + 10;
29  }
30  if (c >= 'A' && c <= 'F') {
31  return c - 'A' + 10;
32  }
33  else if (c >= '0' && c <= '9') {
34  return c - '0';
35  }
36  return -1;
37 }
38 
40 template<typename OutString, typename String>
41 OutString hex_decode_impl(String const& in)
42 {
43  OutString ret;
44  if (!(in.size() % 2)) {
45  ret.reserve(in.size() / 2);
46  for (size_t i = 0; i < in.size(); i += 2) {
47  int high = hex_char_to_int(in[i]);
48  int low = hex_char_to_int(in[i + 1]);
49  if (high == -1 || low == -1) {
50  return OutString();
51  }
52  ret.push_back(static_cast<typename OutString::value_type>((high << 4) + low));
53  }
54  }
55 
56  return ret;
57 }
58 
59 template<typename OutString = std::vector<uint8_t>>
60 OutString hex_decode(std::string_view const& in)
61 {
62  return hex_decode_impl<OutString>(in);
63 }
64 
65 template<typename OutString = std::vector<uint8_t>>
66 OutString hex_decode(std::wstring_view const& in)
67 {
68  return hex_decode_impl<OutString>(in);
69 }
70 
77 template<typename Char = char, bool Lowercase = true>
78 Char int_to_hex_char(int d)
79 {
80  if (d > 9) {
81  return static_cast<Char>((Lowercase ? 'a' : 'A') + d - 10);
82  }
83  else {
84  return static_cast<Char>('0' + d);
85  }
86 }
87 
88 template<typename String, typename InString, bool Lowercase = true>
89 String hex_encode(InString const& data)
90 {
91  static_assert(sizeof(typename InString::value_type) == 1, "Input must be a container of 8 bit values");
92  String ret;
93  ret.reserve(data.size() * 2);
94  for (auto const& c : data) {
95  ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) >> 4));
96  ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) & 0xf));
97  }
98 
99  return ret;
100 }
101 
108 enum class base64_type {
109  standard,
110  url
111 };
112 
114 std::string FZ_PUBLIC_SYMBOL base64_encode(std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
115 std::string FZ_PUBLIC_SYMBOL base64_encode(std::vector<uint8_t> const& in, base64_type type = base64_type::standard, bool pad = true);
116 std::string FZ_PUBLIC_SYMBOL base64_encode(fz::buffer const& in, base64_type type = base64_type::standard, bool pad = true);
117 
124 void FZ_PUBLIC_SYMBOL base64_encode_append(std::string& result, std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
125 
131 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::string_view const& in);
132 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::wstring_view const& in);
133 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(fz::buffer const& in);
134 std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::string_view const& in);
135 std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::wstring_view const& in);
136 std::string FZ_PUBLIC_SYMBOL base64_decode_s(fz::buffer const& in);
137 
138 
145 enum class base32_type {
146  standard,
147  base32hex,
148  locale_safe
149 };
150 
152 std::string FZ_PUBLIC_SYMBOL base32_encode(std::string_view const& in, base32_type type = base32_type::standard, bool pad = true);
153 std::string FZ_PUBLIC_SYMBOL base32_encode(std::vector<uint8_t> const& in, base32_type type = base32_type::standard, bool pad = true);
154 std::string FZ_PUBLIC_SYMBOL base32_encode(fz::buffer const& in, base32_type type = base32_type::standard, bool pad = true);
155 
161 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::string_view const& in, base32_type type = base32_type::standard);
162 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::wstring_view const& in, base32_type type = base32_type::standard);
163 std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(fz::buffer const& in, base32_type type = base32_type::standard);
164 std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::string_view const& in, base32_type type = base32_type::standard);
165 std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::wstring_view const& in, base32_type type = base32_type::standard);
166 std::string FZ_PUBLIC_SYMBOL base32_decode_s(fz::buffer const& in, base32_type type = base32_type::standard);
167 
176 std::string FZ_PUBLIC_SYMBOL percent_encode(std::string_view const& s, bool keep_slashes = false);
177 std::string FZ_PUBLIC_SYMBOL percent_encode(std::wstring_view const& s, bool keep_slashes = false);
178 
184 std::wstring FZ_PUBLIC_SYMBOL percent_encode_w(std::wstring_view const& s, bool keep_slashes = false);
185 
191 std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::string_view const& s, bool allow_embedded_null = false);
192 std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::wstring_view const& s, bool allow_embedded_null = false);
193 std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::string_view const& s, bool allow_embedded_null = false);
194 std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::wstring_view const& s, bool allow_embedded_null = false);
195 
196 }
197 
198 #endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:27
Small class to return filesystem errors.
Definition: fsresult.hpp:26
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17
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::vector< uint8_t > base64_decode(std::string_view const &in)
Decodes base64, ignores whitespace. Returns empty string on invalid input.
int hex_char_to_int(Char c)
Converts a hex digit to decimal int.
Definition: encode.hpp:25
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.
Char int_to_hex_char(int d)
Converts an integer to the corresponding lowercase hex digit.
Definition: encode.hpp:78
base32_type
Alphabet variations for base32.
Definition: encode.hpp:145
std::vector< uint8_t > percent_decode(std::string_view const &s, bool allow_embedded_null=false)
Percent-decodes string.
std::string base32_encode(std::string_view const &in, base32_type type=base32_type::standard, bool pad=true)
Encodes raw input string to base32.
base64_type
Alphabet variations for base64.
Definition: encode.hpp:108
void base64_encode_append(std::string &result, std::string_view const &in, base64_type type=base64_type::standard, bool pad=true)
base64-encodes input and appends it to result.
std::string percent_encode(std::string_view const &s, bool keep_slashes=false)
Percent-encodes string.
std::string base64_encode(std::string_view const &in, base64_type type=base64_type::standard, bool pad=true)
Encodes raw input string to base64.