-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb64fix.cpp
237 lines (202 loc) · 4.94 KB
/
b64fix.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <array>
#include <charconv>
#include <deque>
#include <iostream>
#include <memory>
#include <optional>
#include <utility>
namespace {
constexpr std::array<uint8_t, 64> base64 = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/',
};
template <typename InputIt, typename UnaryPred>
constexpr InputIt find_if_unique(InputIt first, InputIt last, UnaryPred p)
{
InputIt found = last;
for (InputIt it = first; it != last; ++it) {
if (p(*it)) {
if (found != last) {
return last;
}
found = it;
}
}
return found;
}
using Seed = std::array<uint8_t, 3>;
constexpr std::optional<Seed> find_seed()
{
const auto it0 = find_if_unique(base64.begin(), base64.end(), [&](uint8_t c) {
return base64[c >> 2] == c;
});
if (it0 != base64.end()) {
const auto it1 = find_if_unique(base64.begin(), base64.end(), [&](uint8_t c) {
return base64[((*it0 << 4) % 64) | (c >> 4)] == c;
});
if (it1 != base64.end()) {
const auto it2 = find_if_unique(base64.begin(), base64.end(), [&](uint8_t c) {
return base64[((*it1 << 2) % 64) | (c >> 6)] == c;
});
if (it2 != base64.end()) {
return Seed{*it0, *it1, *it2};
}
}
}
return std::nullopt;
}
static_assert(find_seed(), "Seed is expected to be found");
constexpr Seed seed = *find_seed();
static_assert(seed == Seed{'V', 'm', '0'}, "Seed is expected to be 'Vm0'");
class ByteQueue
{
private:
size_t index_;
size_t size_;
size_t max_size_;
std::unique_ptr<uint8_t[]> buffer_;
public:
explicit ByteQueue(size_t max_size) :
index_(0),
size_(0),
max_size_(max_size),
buffer_(std::make_unique<uint8_t[]>(max_size))
{}
bool empty() const
{
return size_ == 0;
}
size_t size() const
{
return size_;
}
uint8_t operator[](size_t i) const
{
return buffer_[(index_ + i) % max_size_];
}
void push_back(uint8_t c)
{
buffer_[(index_ + size_) % max_size_] = c;
++size_;
}
void pop_front()
{
++index_;
--size_;
}
};
class Base64Queue
{
private:
int state_;
ByteQueue bytes_;
public:
Base64Queue(size_t max) :
state_(0),
bytes_((max + 3) / 4)
{}
bool empty() const
{
return bytes_.empty() && state_ == 0;
}
void push_back(uint8_t c)
{
bytes_.push_back(c);
}
uint8_t front() const
{
size_t i;
switch (state_) {
case 0:
i = bytes_[0] >> 2;
break;
case 1:
i = (bytes_[0] << 4) % 64;
if (1 < bytes_.size()) {
i |= bytes_[1] >> 4;
}
break;
case 2:
if (0 < bytes_.size()) {
i = (bytes_[0] << 2) % 64;
if (1 < bytes_.size()) {
i |= bytes_[1] >> 6;
}
} else {
return '=';
}
break;
case 3:
if (0 < bytes_.size()) {
i = bytes_[0] % 64;
} else {
return '=';
}
break;
default:
std::unreachable();
}
return base64[i];
}
void pop_front()
{
switch (state_) {
case 0:
break;
case 1:
bytes_.pop_front();
break;
case 2:
case 3:
if (!bytes_.empty()) {
bytes_.pop_front();
}
break;
default:
std::unreachable();
}
state_ = (state_ + 1) % 4;
}
};
} // namespace
int main(int argc, char** argv)
{
if (argc != 2) {
std::cerr << "usage: b64fix <count>" << std::endl;
return 2;
}
size_t n;
const std::string_view arg(argv[1]);
auto [ptr, ec] = std::from_chars(&*arg.begin(), &*arg.end(), n);
if (ec != std::errc()) {
std::cerr << "b64fix: Failed to parse count: " << arg << std::endl;
return 2;
}
Base64Queue b64q(n);
size_t i = 0;
for (; i < seed.size() && i < n; ++i) {
b64q.push_back(seed[i]);
const uint8_t c = b64q.front();
std::cout.put(c);
b64q.pop_front();
}
for (; i < n; ++i) {
const uint8_t c = b64q.front();
std::cout.put(c);
b64q.pop_front();
b64q.push_back(c);
}
while (!b64q.empty()) {
const uint8_t c = b64q.front();
std::cout.put(c);
b64q.pop_front();
}
std::cout.flush();
return 0;
}