Add base64 tool

This commit is contained in:
2026-08-11 11:34:46 +08:00
parent 3976a8d7e7
commit 0292c0ebb2
10 changed files with 170 additions and 15 deletions
+3 -3
View File
@@ -12,9 +12,9 @@ CTestTestfile.cmake
_deps _deps
CMakeUserPresets.json CMakeUserPresets.json
build/* build
.cache/* .cache
out/* out
# ---> VisualStudioCode # ---> VisualStudioCode
.vscode/* .vscode/*
+19
View File
@@ -0,0 +1,19 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "lldb",
"request": "launch",
// "program": "${command:cmake.launchTargetPath}",
"program": "${workspaceFolder}/out/hello-world",
"args": [],
"cwd": "${workspaceRoot}",
"console": "internalConsole",
"preLaunchTask": "Build",
},
]
}
+20
View File
@@ -0,0 +1,20 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "process",
"command": "/data/data/com.termux/files/usr/bin/g++",
"echoCommand": true,
"isBuildCommand": true,
"args": [
"-g",
"-o",
"${workspaceFolder}/out/hello-world",
"${workspaceFolder}/hello.cpp"
],
},
]
}
-4
View File
@@ -31,7 +31,3 @@ add_executable(
./calender.cpp ./calender.cpp
) )
add_executable(
hello-world
./hello.cpp
)
+9
View File
@@ -0,0 +1,9 @@
add_executable(
encoder
./encode.cpp
)
add_executable(
decoder
./decode.cpp
)
View File
+46
View File
@@ -0,0 +1,46 @@
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <climits>
int main(int argc, char **argv) {
const char *filename = "a.bin";
int bytes_count = 256;
if (argc > 1)
filename = argv[1];
if (argc > 2)
bytes_count = atoi(argv[2]);
auto hex_file = fopen(filename, "wb");
if (hex_file == NULL) {
return -1;
}
srand(time(NULL));
constexpr int chunk_size = 0x400;
unsigned char buffer[chunk_size];
for (int total_bytes = bytes_count; chunk_size < total_bytes; total_bytes -= chunk_size) {
memset(buffer, 0, chunk_size);
for (int i = 0; i < chunk_size; ++ i) {
buffer[i] = rand() % UCHAR_MAX;
}
fwrite(buffer, chunk_size, 1, hex_file);
}
memset(buffer, 0, chunk_size);
for (int i = 0; i < bytes_count % chunk_size; ++ i) {
buffer[i] = rand() % UCHAR_MAX;
}
fwrite(buffer, bytes_count % chunk_size, 1, hex_file);
fclose(hex_file);
return 0;
}
View File
+73
View File
@@ -0,0 +1,73 @@
#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;
}
-8
View File
@@ -1,8 +0,0 @@
#include <cstdio>
#include <cstdlib>
int main(int argc, char *argv[]) {
printf("Hello, world!\n");
return 0;
}