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
15namespace fz {
16
23template<typename Char>
24int 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
39template<typename OutString, typename String>
40OutString 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
58template<typename OutString = std::vector<uint8_t>>
59OutString hex_decode(std::string_view const& in)
60{
61 return hex_decode_impl<OutString>(in);
62}
63
64template<typename OutString = std::vector<uint8_t>>
65OutString hex_decode(std::wstring_view const& in)
66{
67 return hex_decode_impl<OutString>(in);
68}
69
76template<typename Char = char, bool Lowercase = true>
77Char 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
87template<typename String, typename InString, bool Lowercase = true>
88String 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
107enum class base64_type {
108 standard,
109 url
110};
111
113std::string FZ_PUBLIC_SYMBOL base64_encode(std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
114std::string FZ_PUBLIC_SYMBOL base64_encode(std::vector<uint8_t> const& in, base64_type type = base64_type::standard, bool pad = true);
115
122void FZ_PUBLIC_SYMBOL base64_encode_append(std::string& result, std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
123
129std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::string_view const& in);
130std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::wstring_view const& in);
131std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::string_view const& in);
132std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::wstring_view const& in);
133
134
141enum class base32_type {
142 standard,
143 base32hex,
144 locale_safe
145};
146
148std::string FZ_PUBLIC_SYMBOL base32_encode(std::string_view const& in, base32_type type = base32_type::standard, bool pad = true);
149std::string FZ_PUBLIC_SYMBOL base32_encode(std::vector<uint8_t> const& in, base32_type type = base32_type::standard, bool pad = true);
150
156std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::string_view const& in, base32_type type = base32_type::standard);
157std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::wstring_view const& in, base32_type type = base32_type::standard);
158std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::string_view const& in, base32_type type = base32_type::standard);
159std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::wstring_view const& in, base32_type type = base32_type::standard);
160
169std::string FZ_PUBLIC_SYMBOL percent_encode(std::string_view const& s, bool keep_slashes = false);
170std::string FZ_PUBLIC_SYMBOL percent_encode(std::wstring_view const& s, bool keep_slashes = false);
171
177std::wstring FZ_PUBLIC_SYMBOL percent_encode_w(std::wstring_view const& s, bool keep_slashes = false);
178
184std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::string_view const& s, bool allow_embedded_null = false);
185std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::wstring_view const& s, bool allow_embedded_null = false);
186std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::string_view const& s, bool allow_embedded_null = false);
187std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::wstring_view const& s, bool allow_embedded_null = false);
188
189}
190
191#endif
Small class to return filesystem errors.
Definition: local_filesys.hpp:22
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17
std::vector< uint8_t > percent_decode(std::string_view const &s, bool allow_embedded_null=false)
Percent-decodes string.
int hex_char_to_int(Char c)
Converts a hex digit to decimal int.
Definition: encode.hpp:24
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:77
base32_type
Alphabet variations for base32.
Definition: encode.hpp:141
std::vector< uint8_t > base64_decode(std::string_view const &in)
Decodes base64, ignores whitespace. Returns empty string on invalid input.
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:107
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 > 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::string base64_encode(std::string_view const &in, base64_type type=base64_type::standard, bool pad=true)
Encodes raw input string to base64.