-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
202 lines (168 loc) · 6.09 KB
/
Copy pathmain.cpp
File metadata and controls
202 lines (168 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <format>
#include <fstream>
#include <vector>
enum State : unsigned char {
uninitialized = 0,
first,
second,
third,
};
std::byte operator+(std::byte lhs, std::byte rhs) {
return static_cast<std::byte>(static_cast<unsigned char>(lhs) + static_cast<unsigned char>(rhs));
};
std::byte operator-(std::byte lhs, std::byte rhs) {
return static_cast<std::byte>(static_cast<unsigned char>(lhs) - static_cast<unsigned char>(rhs));
};
std::byte base64TableDecoder(std::byte UnRecodedCharacter) {
switch (UnRecodedCharacter) {
case static_cast<std::byte>('+'): {
return static_cast<std::byte>(62);
}
case static_cast<std::byte>('/'): {
return static_cast<std::byte>(63);
}
default: {
if (UnRecodedCharacter <= static_cast<std::byte>(57)) {
return static_cast<std::byte>(52)+(UnRecodedCharacter - static_cast<std::byte>(48));
}
if (UnRecodedCharacter <= static_cast<std::byte>(90)) {
return UnRecodedCharacter - static_cast<std::byte>(65);
}
if (UnRecodedCharacter <= static_cast<std::byte>(122)) {
return static_cast<std::byte>(26)+(UnRecodedCharacter - static_cast<std::byte>(97));
}
throw std::runtime_error("Unrecognized character");
}
}
}
void base64Decoder(std::byte* writeBuffer, std::byte* EndWriteBuffer, std::byte* src, std::size_t srcSize, bool strictMode = true) {
if (srcSize%4 != 0 || *src == static_cast<std::byte>('=')) {
if (strictMode) {
throw std::invalid_argument("srcSize must be a multiple of 4");
} else {
if (*src == static_cast<std::byte>('='))
return;
std::fill_n(src+srcSize, (4-srcSize%4), static_cast<std::byte>('='));
}
}
for (std::size_t i = 0; i < srcSize; i+=4) {
std::byte value{0};
value = base64TableDecoder(src[i])<<2;
if (src[i+1] == static_cast<std::byte>('='))
break;
value |= base64TableDecoder(src[i+1])>>4;
*writeBuffer++ = value;
if (writeBuffer > EndWriteBuffer) {
break;
}
value = base64TableDecoder(src[i+1])<<4;
if (src[i+2] == static_cast<std::byte>('='))
break;
value |= base64TableDecoder(src[i+2])>>2;
*writeBuffer++ = value;
if (writeBuffer > EndWriteBuffer) {
break;
}
value = base64TableDecoder(src[i+2])<<6;
if (src[i+3] == static_cast<std::byte>('='))
break;
value |= base64TableDecoder(src[i+3]);
*writeBuffer++ = value;
if (writeBuffer > EndWriteBuffer || (i+4 < srcSize) && src[i+4] == static_cast<std::byte>('=')) {
break;
}
}
}
std::byte base64TableEncoder(std::byte recodedCharacter) {
if (static_cast<std::byte>(0) <= recodedCharacter && recodedCharacter <= static_cast<std::byte>(25)) {
return static_cast<std::byte>(65)+recodedCharacter;
}
if (recodedCharacter >= static_cast<std::byte>(26) && recodedCharacter <= static_cast<std::byte>(51)) {
return static_cast<std::byte>(97)+(recodedCharacter-static_cast<std::byte>(26));
}
if (recodedCharacter >= static_cast<std::byte>(52) && recodedCharacter <= static_cast<std::byte>(61))
return static_cast<std::byte>(48)+(recodedCharacter-static_cast<std::byte>(52));
switch (recodedCharacter) {
case static_cast<std::byte>(62):
return static_cast<std::byte>('+');
case static_cast<std::byte>(63):
return static_cast<std::byte>('/');
}
return recodedCharacter;
}
std::byte LeftShiftForBase(std::byte recodCharacter, unsigned char PosNow) {
return static_cast<std::byte>(recodCharacter<<(8-PosNow))>>2;
}
std::string base64Encode(const std::byte* __restrict str, const std::size_t lenStr) {
if (str == nullptr || lenStr == 0)
return "";
std::string out;
std::byte recoded = static_cast<std::byte>(0);
State state = first;
for (std::size_t i = 0; i < lenStr; i++) {
switch (state) {
case first: {
recoded = LeftShiftForBase(str[i], 8);
out += static_cast<char>(base64TableEncoder(recoded));
recoded = LeftShiftForBase(str[i], 2);
state = second;
break;
}
case second: {
recoded |= str[i]>>4;
out += static_cast<char>(base64TableEncoder(recoded));
recoded = LeftShiftForBase(str[i], 4);
state = third;
break;
}
case third: {
recoded |= str[i]>>6;
out += static_cast<char>(base64TableEncoder(recoded));
recoded = LeftShiftForBase(str[i], 6);
out += static_cast<char>(base64TableEncoder(recoded));
state = first;
break;
}
default: {
};
}
}
if (state > first)
out += static_cast<char>(base64TableEncoder(recoded));
switch (lenStr%3) {
case 1: {
out += "==";
break;
}
case 2: {
out += "=";
break;
}
default: {};
}
return out;
}
void testEncoding() {
std::string str = "";
std::cout << "Enter a string to encoding" << std::endl;
std::getline(std::cin, str);
std::cout << base64Encode(reinterpret_cast<const std::byte *>(str.data()), str.length()) << std::endl;
}
void testDecoding() {
std::string str;
std::cout << "Enter a string to decoding" << std::endl;
std::getline(std::cin, str);
std::string out;
out.resize(std::ceil(str.size()/4)*3);
base64Decoder(reinterpret_cast<std::byte *>(out.data()), reinterpret_cast<std::byte *>(out.data()+out.size()), reinterpret_cast<std::byte *>(str.data()), str.size());
std::cout << out << std::endl;
}
int main() {
// testEncoding();
// testDecoding();
return 0;
}