libfilezilla
Loading...
Searching...
No Matches
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#include <cstdint>
9
15
16namespace fz {
17class buffer;
18class secure_buffer;
19
26template<typename Char>
27int hex_char_to_int(Char c)
28{
29 if (c >= 'a' && c <= 'f') {
30 return c - 'a' + 10;
31 }
32 if (c >= 'A' && c <= 'F') {
33 return c - 'A' + 10;
34 }
35 else if (c >= '0' && c <= '9') {
36 return c - '0';
37 }
38 return -1;
39}
40
42template<typename OutString, typename String>
43OutString hex_decode_impl(String const& in)
44{
45 OutString ret;
46 if (!(in.size() % 2)) {
47 ret.reserve(in.size() / 2);
48 for (size_t i = 0; i < in.size(); i += 2) {
49 int high = hex_char_to_int(in[i]);
50 int low = hex_char_to_int(in[i + 1]);
51 if (high == -1 || low == -1) {
52 return OutString();
53 }
54 ret.push_back(static_cast<typename OutString::value_type>((high << 4) + low));
55 }
56 }
57
58 return ret;
59}
60
61template<typename OutString = std::vector<uint8_t>>
62OutString hex_decode(std::string_view const& in)
63{
64 return hex_decode_impl<OutString>(in);
65}
66
67template<typename OutString = std::vector<uint8_t>>
68OutString hex_decode(std::wstring_view const& in)
69{
70 return hex_decode_impl<OutString>(in);
71}
72
79template<typename Char = char, bool Lowercase = true>
80Char int_to_hex_char(int d)
81{
82 if (d > 9) {
83 return static_cast<Char>((Lowercase ? 'a' : 'A') + d - 10);
84 }
85 else {
86 return static_cast<Char>('0' + d);
87 }
88}
89
90template<typename String, typename InString, bool Lowercase = true>
91String hex_encode(InString const& data)
92{
93 static_assert(sizeof(typename InString::value_type) == 1, "Input must be a container of 8 bit values");
94 String ret;
95 ret.reserve(data.size() * 2);
96 for (auto const& c : data) {
97 ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) >> 4));
98 ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) & 0xf));
99 }
100
101 return ret;
102}
103
110enum class base64_type {
111 standard,
112 url
113};
114
116std::string FZ_PUBLIC_SYMBOL base64_encode(std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
117std::string FZ_PUBLIC_SYMBOL base64_encode(std::vector<uint8_t> const& in, base64_type type = base64_type::standard, bool pad = true);
118std::string FZ_PUBLIC_SYMBOL base64_encode(buffer const& in, base64_type type = base64_type::standard, bool pad = true);
119std::string FZ_PUBLIC_SYMBOL base64_encode(secure_buffer const& in, base64_type type = base64_type::standard, bool pad = true);
120
127void FZ_PUBLIC_SYMBOL base64_encode_append(std::string& result, std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
128
134std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::string_view const& in);
135std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::wstring_view const& in);
136std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(buffer const& in);
137std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(secure_buffer const& in);
138std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::string_view const& in);
139std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::wstring_view const& in);
140std::string FZ_PUBLIC_SYMBOL base64_decode_s(secure_buffer const& in);
141
142
149enum class base32_type {
150 standard,
151 base32hex,
152 locale_safe
153};
154
156std::string FZ_PUBLIC_SYMBOL base32_encode(std::string_view const& in, base32_type type = base32_type::standard, bool pad = true);
157std::string FZ_PUBLIC_SYMBOL base32_encode(std::vector<uint8_t> const& in, base32_type type = base32_type::standard, bool pad = true);
158std::string FZ_PUBLIC_SYMBOL base32_encode(buffer const& in, base32_type type = base32_type::standard, bool pad = true);
159std::string FZ_PUBLIC_SYMBOL base32_encode(secure_buffer const& in, base32_type type = base32_type::standard, bool pad = true);
160
166std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::string_view const& in, base32_type type = base32_type::standard);
167std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::wstring_view const& in, base32_type type = base32_type::standard);
168std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(buffer const& in, base32_type type = base32_type::standard);
169std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(secure_buffer const& in, base32_type type = base32_type::standard);
170std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::string_view const& in, base32_type type = base32_type::standard);
171std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::wstring_view const& in, base32_type type = base32_type::standard);
172std::string FZ_PUBLIC_SYMBOL base32_decode_s(buffer const& in, base32_type type = base32_type::standard);
173std::string FZ_PUBLIC_SYMBOL base32_decode_s(secure_buffer const& in, base32_type type = base32_type::standard);
174
183std::string FZ_PUBLIC_SYMBOL percent_encode(std::string_view const& s, bool keep_slashes = false);
184std::string FZ_PUBLIC_SYMBOL percent_encode(std::wstring_view const& s, bool keep_slashes = false);
185
191std::wstring FZ_PUBLIC_SYMBOL percent_encode_w(std::wstring_view const& s, bool keep_slashes = false);
192
198std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::string_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
199std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::wstring_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
200std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::string_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
201std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::wstring_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
202
203}
204
205#endif
Definition buffer.hpp:201
Small class to return filesystem errors.
Definition fsresult.hpp:26
Definition buffer.hpp:210
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.
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::vector< uint8_t > base64_decode(std::string_view const &in)
Decodes base64, ignores whitespace. Returns empty string on invalid input.
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.
std::string base64_encode(std::string_view const &in, base64_type type=base64_type::standard, bool pad=true)
Encodes raw input string to base64.
std::string base32_encode(std::string_view const &in, base32_type type=base32_type::standard, bool pad=true)
Encodes raw input string to base32.
Char int_to_hex_char(int d)
Converts an integer to the corresponding lowercase hex digit.
Definition encode.hpp:80
base32_type
Alphabet variations for base32.
Definition encode.hpp:149
std::string percent_encode(std::string_view const &s, bool keep_slashes=false)
Percent-encodes string.
base64_type
Alphabet variations for base64.
Definition encode.hpp:110
std::vector< uint8_t > percent_decode(std::string_view const &s, bool allow_embedded_null=false, bool plus_is_space=false)
Percent-decodes string.
int hex_char_to_int(Char c)
Converts a hex digit to decimal int.
Definition encode.hpp:27