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 <= 'z') {
27  return c - 'a' + 10;
28  }
29  if (c >= 'A' && c <= 'Z') {
30  return c - 'A' + 10;
31  }
32  else if (c >= '0' && c <= '9') {
33  return c - '0';
34  }
35  return -1;
36 }
37 
38 template<typename String>
39 std::vector<uint8_t> hex_decode(String const& in)
40 {
41  std::vector<uint8_t> ret;
42  if (!(in.size() % 2)) {
43  ret.reserve(in.size() / 2);
44  for (size_t i = 0; i < in.size(); i += 2) {
45  int high = hex_char_to_int(in[i]);
46  int low = hex_char_to_int(in[i + 1]);
47  if (high == -1 || low == -1) {
48  return std::vector<uint8_t>();
49  }
50  ret.push_back(static_cast<uint8_t>((high << 4) + low));
51  }
52  }
53 
54  return ret;
55 }
56 
63 template<typename Char = char, bool Lowercase = true>
64 Char int_to_hex_char(int d)
65 {
66  if (d > 9) {
67  return static_cast<Char>((Lowercase ? 'a' : 'A') + d - 10);
68  }
69  else {
70  return static_cast<Char>('0' + d);
71  }
72 }
73 
74 template<typename String, typename InString, bool Lowercase = true>
75 String hex_encode(InString const& data)
76 {
77  static_assert(sizeof(typename InString::value_type) == 1, "Input must be a container of 8 bit values");
78  String ret;
79  ret.reserve(data.size() * 2);
80  for (auto const& c : data) {
81  ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) >> 4));
82  ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) & 0xf));
83  }
84 
85  return ret;
86 }
87 
94 enum class base64_type {
95  standard,
96  url
97 };
98 
100 std::string FZ_PUBLIC_SYMBOL base64_encode(std::string const& in, base64_type type = base64_type::standard, bool pad = true);
101 
107 std::string FZ_PUBLIC_SYMBOL base64_decode(std::string const& in);
108 
109 
118 std::string FZ_PUBLIC_SYMBOL percent_encode(std::string const& s, bool keep_slashes = false);
119 std::string FZ_PUBLIC_SYMBOL percent_encode(std::wstring const& s, bool keep_slashes = false);
120 
126 std::wstring FZ_PUBLIC_SYMBOL percent_encode_w(std::wstring const& s, bool keep_slashes = false);
127 
133 std::string FZ_PUBLIC_SYMBOL percent_decode(std::string const& s);
134 
135 }
136 
137 #endif
std::wstring percent_encode_w(std::wstring const &s, bool keep_slashes=false)
Percent-enodes wide-character. Non-ASCII characters are converted to UTF-8 befor they are encoded...
Char int_to_hex_char(int d)
Converts an integer to the corresponding lowercase hex digit.
Definition: encode.hpp:64
base64_type
Alphabet variations for base64.
Definition: encode.hpp:94
int hex_char_to_int(Char c)
Converts a hex digit to decimal int.
Definition: encode.hpp:24
std::string percent_decode(std::string const &s)
Percent-decodes string.
std::string percent_encode(std::string const &s, bool keep_slashes=false)
Percent-enodes string.
The namespace used by libfilezilla.
Definition: apply.hpp:16
Sets some global macros and further includes string.hpp.
std::string base64_encode(std::string const &in, base64_type type=base64_type::standard, bool pad=true)
Encodes raw input string to base64.
std::string base64_decode(std::string const &in)
Decodes base64, ignores whitespace. Returns empty string on invalid input.