This commit is contained in:
Your Name 2025-05-12 19:48:56 +12:00
parent 20fc331bd2
commit bb355cf4ac
6 changed files with 169 additions and 77 deletions

68
.vscode/settings.json vendored Normal file

@ -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"
}
}

42
b64ed.cpp Normal file

@ -0,0 +1,42 @@
#include "b64ed.hpp"
#include <vector>
// 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<int> 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;
}

9
b64ed.hpp Normal file

@ -0,0 +1,9 @@
#ifndef B64ED_HPP
#define B64ED_HPP
#include <string>
std::string base64_decode(const std::string &in);
std::string base64_encode(const std::string &in);
#endif

119
bb64.cpp

@ -6,103 +6,57 @@
#include <sstream>
#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<int> 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;
}
}
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;
};
}

@ -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

@ -1 +1 @@
static const char *VERSION = "5";
static const char *VERSION = "8";