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;
18
25template<typename Char>
26int hex_char_to_int(Char c)
27{
28 if (c >= 'a' && c <= 'f') {
29 return c - 'a' + 10;
30 }
31 if (c >= 'A' && c <= 'F') {
32 return c - 'A' + 10;
33 }
34 else if (c >= '0' && c <= '9') {
35 return c - '0';
36 }
37 return -1;
38}
39
41template<typename OutString, typename String>
42OutString hex_decode_impl(String const& in)
43{
44 OutString ret;
45 if (!(in.size() % 2)) {
46 ret.reserve(in.size() / 2);
47 for (size_t i = 0; i < in.size(); i += 2) {
48 int high = hex_char_to_int(in[i]);
49 int low = hex_char_to_int(in[i + 1]);
50 if (high == -1 || low == -1) {
51 return OutString();
52 }
53 ret.push_back(static_cast<typename OutString::value_type>((high << 4) + low));
54 }
55 }
56
57 return ret;
58}
59
60template<typename OutString = std::vector<uint8_t>>
61OutString hex_decode(std::string_view const& in)
62{
63 return hex_decode_impl<OutString>(in);
64}
65
66template<typename OutString = std::vector<uint8_t>>
67OutString hex_decode(std::wstring_view const& in)
68{
69 return hex_decode_impl<OutString>(in);
70}
71
78template<typename Char = char, bool Lowercase = true>
79Char int_to_hex_char(int d)
80{
81 if (d > 9) {
82 return static_cast<Char>((Lowercase ? 'a' : 'A') + d - 10);
83 }
84 else {
85 return static_cast<Char>('0' + d);
86 }
87}
88
89template<typename String, typename InString, bool Lowercase = true>
90String hex_encode(InString const& data)
91{
92 static_assert(sizeof(typename InString::value_type) == 1, "Input must be a container of 8 bit values");
93 String ret;
94 ret.reserve(data.size() * 2);
95 for (auto const& c : data) {
96 ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) >> 4));
97 ret.push_back(int_to_hex_char<typename String::value_type, Lowercase>(static_cast<unsigned char>(c) & 0xf));
98 }
99
100 return ret;
101}
102
109enum class base64_type {
110 standard,
111 url
112};
113
115std::string FZ_PUBLIC_SYMBOL base64_encode(std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
116std::string FZ_PUBLIC_SYMBOL base64_encode(std::vector<uint8_t> const& in, base64_type type = base64_type::standard, bool pad = true);
117std::string FZ_PUBLIC_SYMBOL base64_encode(fz::buffer const& in, base64_type type = base64_type::standard, bool pad = true);
118
125void FZ_PUBLIC_SYMBOL base64_encode_append(std::string& result, std::string_view const& in, base64_type type = base64_type::standard, bool pad = true);
126
132std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::string_view const& in);
133std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(std::wstring_view const& in);
134std::vector<uint8_t> FZ_PUBLIC_SYMBOL base64_decode(fz::buffer const& in);
135std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::string_view const& in);
136std::string FZ_PUBLIC_SYMBOL base64_decode_s(std::wstring_view const& in);
137std::string FZ_PUBLIC_SYMBOL base64_decode_s(fz::buffer const& in);
138
139
146enum class base32_type {
147 standard,
148 base32hex,
149 locale_safe
150};
151
153std::string FZ_PUBLIC_SYMBOL base32_encode(std::string_view const& in, base32_type type = base32_type::standard, bool pad = true);
154std::string FZ_PUBLIC_SYMBOL base32_encode(std::vector<uint8_t> const& in, base32_type type = base32_type::standard, bool pad = true);
155std::string FZ_PUBLIC_SYMBOL base32_encode(fz::buffer const& in, base32_type type = base32_type::standard, bool pad = true);
156
162std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::string_view const& in, base32_type type = base32_type::standard);
163std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(std::wstring_view const& in, base32_type type = base32_type::standard);
164std::vector<uint8_t> FZ_PUBLIC_SYMBOL base32_decode(fz::buffer const& in, base32_type type = base32_type::standard);
165std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::string_view const& in, base32_type type = base32_type::standard);
166std::string FZ_PUBLIC_SYMBOL base32_decode_s(std::wstring_view const& in, base32_type type = base32_type::standard);
167std::string FZ_PUBLIC_SYMBOL base32_decode_s(fz::buffer const& in, base32_type type = base32_type::standard);
168
177std::string FZ_PUBLIC_SYMBOL percent_encode(std::string_view const& s, bool keep_slashes = false);
178std::string FZ_PUBLIC_SYMBOL percent_encode(std::wstring_view const& s, bool keep_slashes = false);
179
185std::wstring FZ_PUBLIC_SYMBOL percent_encode_w(std::wstring_view const& s, bool keep_slashes = false);
186
192std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::string_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
193std::vector<uint8_t> FZ_PUBLIC_SYMBOL percent_decode(std::wstring_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
194std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::string_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
195std::string FZ_PUBLIC_SYMBOL percent_decode_s(std::wstring_view const& s, bool allow_embedded_null = false, bool plus_is_space = false);
196
197}
198
199#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:28
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.
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:79
base32_type
Alphabet variations for base32.
Definition encode.hpp:146
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:109
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:26