From bb355cf4ac142d9b0816411afff25cbc4e852166 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 12 May 2025 19:48:56 +1200 Subject: [PATCH] Tidy --- .vscode/settings.json | 68 ++++++++++++++++++++++++ b64ed.cpp | 42 +++++++++++++++ b64ed.hpp | 9 ++++ bb64.cpp | 119 ++++++++++++++++-------------------------- build.sh | 6 +-- version.h | 2 +- 6 files changed, 169 insertions(+), 77 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 b64ed.cpp create mode 100644 b64ed.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9402bed --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,68 @@ +{ + "files.associations": { + "*.inja": "jinja-html", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp", + "variant": "cpp", + "format": "cpp", + "__nullptr": "cpp" + } +} \ No newline at end of file diff --git a/b64ed.cpp b/b64ed.cpp new file mode 100644 index 0000000..cec8e1f --- /dev/null +++ b/b64ed.cpp @@ -0,0 +1,42 @@ +#include "b64ed.hpp" + +#include + +// Custom base64 encoding/decoding tables +static const std::string custom_base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+_"; + +std::string base64_encode(const std::string &in) { + std::string out; + int val = 0, valb = -6; + for (unsigned char c : in) { + val = (val << 8) + c; + valb += 8; + while (valb >= 0) { + out.push_back(custom_base64_chars[(val >> valb) & 0x3F]); + valb -= 6; + } + } + if (valb > -6) out.push_back(custom_base64_chars[((val << 8) >> (valb + 8)) & 0x3F]); + while (out.size() % 4) out.push_back('='); + return out; +} + +std::string base64_decode(const std::string &in) { + std::vector T(256, -1); + for (int i = 0; i < 64; i++) T[custom_base64_chars[i]] = i; + std::string out; + int val = 0, valb = -8; + for (unsigned char c : in) { + if (T[c] == -1) break; + val = (val << 6) + T[c]; + valb += 6; + if (valb >= 0) { + out.push_back(char((val >> valb) & 0xFF)); + valb -= 8; + } + } + return out; +} \ No newline at end of file diff --git a/b64ed.hpp b/b64ed.hpp new file mode 100644 index 0000000..fa71d44 --- /dev/null +++ b/b64ed.hpp @@ -0,0 +1,9 @@ +#ifndef B64ED_HPP +#define B64ED_HPP + +#include + +std::string base64_decode(const std::string &in); +std::string base64_encode(const std::string &in); + +#endif diff --git a/bb64.cpp b/bb64.cpp index 3fa00bc..7abd5bd 100644 --- a/bb64.cpp +++ b/bb64.cpp @@ -6,103 +6,57 @@ #include #include "version.h" - -// Custom base64 encoding/decoding tables -static const std::string base64_chars = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - -std::string base64_encode(const std::string &in) { - std::string out; - int val = 0, valb = -6; - for (unsigned char c : in) { - val = (val << 8) + c; - valb += 8; - while (valb >= 0) { - out.push_back(base64_chars[(val >> valb) & 0x3F]); - valb -= 6; - } - } - if (valb > -6) out.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]); - while (out.size() % 4) out.push_back('='); - return out; -} - -std::string base64_decode(const std::string &in) { - std::vector T(256, -1); - for (int i = 0; i < 64; i++) T[base64_chars[i]] = i; - std::string out; - int val = 0, valb = -8; - for (unsigned char c : in) { - if (T[c] == -1) break; - val = (val << 6) + T[c]; - valb += 6; - if (valb >= 0) { - out.push_back(char((val >> valb) & 0xFF)); - valb -= 8; - } - } - return out; -} +#include "b64ed.hpp" // Recursively decode and print if nested bb64 command is found -void recursive_print(const std::string &decoded) { +void recursive_print(const std::string &decoded) +{ std::cout << decoded << std::endl; + size_t pos = decoded.find("bb64 "); - if (pos != std::string::npos) { + if (pos != std::string::npos) + { std::istringstream iss(decoded.substr(pos)); std::string cmd, arg; iss >> cmd >> arg; - if (cmd == "bb64" && !arg.empty()) { + if (cmd == "bb64" && !arg.empty()) + { std::string nested = base64_decode(arg); + std::cout << " "; + std::cout << "nested: " << nested << std::endl; recursive_print(nested); } } } -int main(int argc, char *argv[]) { - if (argc < 2) { +constexpr unsigned int hash(const char *s, int off = 0) +{ + return !s[off] ? 5381 : (hash(s, off + 1) * 33) ^ s[off]; +} + +int main(int argc, char *argv[]) +{ + if (argc < 2) + { std::cerr << "bb64 version " << VERSION << ", by J842." << std::endl; // heredoc for instructions std::cerr << R"( Usage: bb64 BASE64COMMAND Decodes and runs the command - bb64 -i BASE64COMMAND Displays the decoded command + bb64 -[i|d] BASE64COMMAND Displays the decoded command bb64 -e COMMAND Encodes the command and prints the result )" << std::endl; return -1; } - std::string mode; - int argi = 1; - if (std::string(argv[1]) == "-i" || std::string(argv[1]) == "-e") { - mode = argv[1]; - argi = 2; - } - if (mode == "-e") { - // Encode the rest of the arguments as a single command string - std::ostringstream oss; - for (int i = argi; i < argc; ++i) { - if (i > argi) oss << " "; - oss << argv[i]; - } - std::string encoded = base64_encode(oss.str()); - std::cout << encoded << std::endl; - return 0; - } else if (mode == "-i") { - if (argi >= argc) { - std::cerr << "No BASE64COMMAND provided for -i" << std::endl; - return -1; - } - std::string decoded = base64_decode(argv[argi]); - recursive_print(decoded); - return 0; - } else { + + if (argc == 2) + { // Default: decode and run - std::string decoded = base64_decode(argv[argi]); - if (decoded.empty()) { + std::string decoded = base64_decode(argv[1]); + if (decoded.empty()) + { std::cerr << "Failed to decode base64 command." << std::endl; return -1; } @@ -112,4 +66,23 @@ Usage: std::cerr << "Failed to execute command." << std::endl; return -1; } -} \ No newline at end of file + + std::string mode = argv[1]; + std::ostringstream oss; + + switch (hash(mode.c_str())) + { + case hash("-i"): + case hash("-d"): + recursive_print(base64_decode(argv[2])); + break; + case hash("-e"): + for (int i = 2; i < argc; ++i) + oss << (i > 2 ? " " : "") << argv[i]; + std::cout << base64_encode(oss.str()) << std::endl; + break; + default: + std::cerr << "Invalid mode: " << mode << std::endl; + return -1; + }; +} \ No newline at end of file diff --git a/build.sh b/build.sh index 130b607..bc13373 100755 --- a/build.sh +++ b/build.sh @@ -9,9 +9,9 @@ mkdir -p "$OUTPUT_DIR" if [[ $(uname -m) == "x86_64" ]]; then echo "Building for x86_64 (musl static)..." if command -v x86_64-linux-musl-g++ &>/dev/null; then - x86_64-linux-musl-g++ -O2 -static -o "$OUTPUT_DIR/bb64.amd64" bb64.cpp + x86_64-linux-musl-g++ -O2 -static -o "$OUTPUT_DIR/bb64.amd64" bb64.cpp b64ed.cpp else - g++ -O2 -static -o "$OUTPUT_DIR/bb64.amd64" bb64.cpp -static-libgcc -static-libstdc++ + g++ -O2 -static -o "$OUTPUT_DIR/bb64.amd64" bb64.cpp b64ed.cpp -static-libgcc -static-libstdc++ echo "Warning: musl-g++ not found, built with g++ static flags." fi echo "Built bb64.amd64 (x86_64, static)" @@ -20,6 +20,6 @@ fi # Build for arm64 (musl static) if cross-compiler available if command -v aarch64-linux-musl-g++ &>/dev/null; then echo "Building for arm64 (musl static)..." - aarch64-linux-musl-g++ -O2 -static -o "$OUTPUT_DIR/bb64.arm64" bb64.cpp + aarch64-linux-musl-g++ -O2 -static -o "$OUTPUT_DIR/bb64.arm64" bb64.cpp b64ed.cpp echo "Built bb64.arm64 (arm64, static)" fi \ No newline at end of file diff --git a/version.h b/version.h index f0e18c9..4e79690 100644 --- a/version.h +++ b/version.h @@ -1 +1 @@ -static const char *VERSION = "5"; +static const char *VERSION = "8";