diff --git a/.gitignore b/.gitignore index 265bddf..51b9b41 100644 --- a/.gitignore +++ b/.gitignore @@ -12,9 +12,9 @@ CTestTestfile.cmake _deps CMakeUserPresets.json -build/* -.cache/* -out/* +build +.cache +out # ---> VisualStudioCode .vscode/* diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..dbd1f93 --- /dev/null +++ b/.vscode/launch.json @@ -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", + }, + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a4da35d --- /dev/null +++ b/.vscode/tasks.json @@ -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" + ], + }, + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 405c2bc..75c2f3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,3 @@ add_executable( ./calender.cpp ) -add_executable( - hello-world - ./hello.cpp -) diff --git a/base64/CMakeLists.txt b/base64/CMakeLists.txt new file mode 100644 index 0000000..742a411 --- /dev/null +++ b/base64/CMakeLists.txt @@ -0,0 +1,9 @@ +add_executable( + encoder + ./encode.cpp +) + +add_executable( + decoder + ./decode.cpp +) diff --git a/base64/base64.hpp b/base64/base64.hpp new file mode 100644 index 0000000..e69de29 diff --git a/base64/binary.cpp b/base64/binary.cpp new file mode 100644 index 0000000..0a1e09b --- /dev/null +++ b/base64/binary.cpp @@ -0,0 +1,46 @@ +#include +#include + +#include +#include + +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; +} diff --git a/base64/decode.cpp b/base64/decode.cpp new file mode 100644 index 0000000..e69de29 diff --git a/base64/encode.cpp b/base64/encode.cpp new file mode 100644 index 0000000..e073743 --- /dev/null +++ b/base64/encode.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +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 + iter_cur(file), + iter_end; + + std::array raw; + std::array 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; +} diff --git a/hello.cpp b/hello.cpp deleted file mode 100644 index 834d644..0000000 --- a/hello.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -int main(int argc, char *argv[]) { - printf("Hello, world!\n"); - - return 0; -} \ No newline at end of file