Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
3#include <ctype.h>
4/*
5 base64.cpp and base64.h
6
7 Copyright (C) 2004-2008 René Nyffenegger
8
9 This source code is provided 'as-is', without any express or implied
10 warranty. In no event will the author be held liable for any damages
11 arising from the use of this software.
12
13 Permission is granted to anyone to use this software for any purpose,
14 including commercial applications, and to alter it and redistribute it
15 freely, subject to the following restrictions:
16
17 1. The origin of this source code must not be misrepresented; you must not
18 claim that you wrote the original source code. If you use this source code
19 in a product, an acknowledgment in the product documentation would be
20 appreciated but is not required.
21
22 2. Altered source versions must be plainly marked as such, and must not be
23 misrepresented as being the original source code.
24
25 3. This notice may not be removed or altered from any source distribution.
26
27 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
28
29*/
30
31namespace fc {
32
33static constexpr char base64_chars[] =
34 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35 "abcdefghijklmnopqrstuvwxyz"
36 "0123456789+/";
37static constexpr char base64url_chars[] =
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
39 "abcdefghijklmnopqrstuvwxyz"
40 "0123456789-_";
41
42static_assert(sizeof(base64_chars) == sizeof(base64url_chars), "base64 and base64url must have the same amount of chars");
43
44static inline void throw_on_nonbase64(unsigned char c, const char* const b64_chars) {
45 FC_ASSERT(isalnum(c) || (c == b64_chars[sizeof(base64_chars)-3]) || (c == b64_chars[sizeof(base64_chars)-2]), "encountered non-base64 character");
46}
47
48std::string base64_encode_impl(unsigned char const* bytes_to_encode, unsigned int in_len, const char* const b64_chars) {
49
50 std::string ret;
51 int i = 0;
52 int j = 0;
53 unsigned char char_array_3[3];
54 unsigned char char_array_4[4];
55
56 while (in_len--) {
57 char_array_3[i++] = *(bytes_to_encode++);
58 if (i == 3) {
59 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
60 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
61 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
62 char_array_4[3] = char_array_3[2] & 0x3f;
63
64 for(i = 0; (i <4) ; i++)
65 ret += b64_chars[char_array_4[i]];
66 i = 0;
67 }
68 }
69
70 if (i)
71 {
72 for(j = i; j < 3; j++)
73 char_array_3[j] = '\0';
74
75 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
76 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
77 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
78 char_array_4[3] = char_array_3[2] & 0x3f;
79
80 for (j = 0; (j < i + 1); j++)
81 ret += b64_chars[char_array_4[j]];
82
83 while((i++ < 3))
84 ret += '=';
85
86 }
87
88 return ret;
89
90}
91
92std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
93 return base64_encode_impl(bytes_to_encode, in_len, base64_chars);
94}
95
96std::string base64_encode( const std::string& enc ) {
97 char const* s = enc.c_str();
98 return base64_encode( (unsigned char const*)s, enc.size() );
99}
100
101std::string base64url_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
102 return base64_encode_impl(bytes_to_encode, in_len, base64url_chars);
103}
104
105std::string base64url_encode( const std::string& enc ) {
106 char const* s = enc.c_str();
107 return base64url_encode( (unsigned char const*)s, enc.size() );
108}
109
110std::string base64_decode_impl(std::string const& encoded_string, const char* const b64_chars) {
111 int in_len = encoded_string.size();
112 int i = 0;
113 int j = 0;
114 int in_ = 0;
115 unsigned char char_array_4[4], char_array_3[3];
116 std::string ret;
117
118 while (in_len-- && encoded_string[in_] != '=') {
119 throw_on_nonbase64(encoded_string[in_], b64_chars);
120 char_array_4[i++] = encoded_string[in_]; in_++;
121 if (i ==4) {
122 for (i = 0; i <4; i++)
123 char_array_4[i] = strchr(b64_chars, char_array_4[i]) - b64_chars;
124
125 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
126 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
127 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
128
129 for (i = 0; (i < 3); i++)
130 ret += char_array_3[i];
131 i = 0;
132 }
133 }
134
135 if (i) {
136 for (j = i; j <4; j++)
137 char_array_4[j] = 0;
138
139 for (j = 0; j <4; j++)
140 char_array_4[j] = strchr(b64_chars, char_array_4[j]) - b64_chars;
141
142 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
143 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
144 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
145
146 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
147 }
148
149 return ret;
150}
151
152std::string base64_decode(std::string const& encoded_string) {
153 return base64_decode_impl(encoded_string, base64_chars);
154}
155
156std::string base64url_decode(std::string const& encoded_string) {
157 return base64_decode_impl(encoded_string, base64url_chars);
158}
159
160} // namespace fc
161
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
namespace sysio::chain
Definition authority.cpp:3
std::string base64_decode_impl(std::string const &encoded_string, const char *const b64_chars)
Definition base64.cpp:110
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition base64.cpp:92
std::string base64url_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition base64.cpp:101
std::string base64url_decode(const std::string &encoded_string)
Definition base64.cpp:156
std::string base64_encode_impl(unsigned char const *bytes_to_encode, unsigned int in_len, const char *const b64_chars)
Definition base64.cpp:48
std::string base64_decode(const std::string &encoded_string)
Definition base64.cpp:152
CK_RV ret
char * s
uint16_t j
size_t in_len