74 lines
1.7 KiB
C++
74 lines
1.7 KiB
C++
#include <array>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <string>
|
|
|
|
constexpr char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
int main(int argc, char *argv[]) {
|
|
std::string
|
|
filename("a.bin"),
|
|
textfile("a.txt");
|
|
|
|
if (argc > 1) {
|
|
filename.assign(argv[1]);
|
|
}
|
|
|
|
if (argc > 2) {
|
|
textfile.assign(argv[2]);
|
|
}
|
|
|
|
std::fstream
|
|
file(filename, std::ios::binary | std::ios::in),
|
|
text(textfile, std::ios::out);
|
|
|
|
std::istreambuf_iterator<char>
|
|
iter_cur(file),
|
|
iter_end;
|
|
|
|
std::array<unsigned char, 0x1000> raw;
|
|
std::array<unsigned char, 0x2000> b64;
|
|
while (iter_cur != iter_end) {
|
|
raw.fill(0);
|
|
b64.fill(0);
|
|
|
|
auto beg = raw.begin();
|
|
auto cur = beg;
|
|
auto end = raw.end();
|
|
|
|
while (cur != end && iter_cur != iter_end) {
|
|
*cur++ = *iter_cur++;
|
|
}
|
|
|
|
auto delta = cur - beg;
|
|
auto repeat = delta / 3 + (delta % 3 != 0);
|
|
auto bin_ptr = beg;
|
|
auto b64_ptr = b64.begin();
|
|
|
|
for (auto a = 0; a < repeat; ++ a, bin_ptr += 3, b64_ptr += 4) {
|
|
int hex[4] = {
|
|
(bin_ptr[0] & 0b11111100) >> 2,
|
|
((bin_ptr[0] & 0b00000011) << 4) +
|
|
((bin_ptr[1] & 0b11110000) >> 4),
|
|
((bin_ptr[1] & 0b00001111) << 2) +
|
|
((bin_ptr[2] & 0b11000000) >> 6),
|
|
(bin_ptr[2] & 0b00111111),
|
|
};
|
|
|
|
for (int i = 0; i < 4; ++ i) {
|
|
b64_ptr[i] = base64[hex[i]];
|
|
}
|
|
}
|
|
|
|
for (int i = 1; i < delta % 3; ++ i) {
|
|
b64_ptr[-i] = '=';
|
|
}
|
|
|
|
text << b64.data();
|
|
}
|
|
|
|
text.flush();
|
|
|
|
return 0;
|
|
}
|