.
This commit is contained in:
parent
d001825cd5
commit
d544b8953d
55
bb64.cpp
55
bb64.cpp
@ -4,14 +4,16 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <filesystem>
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
#include "b64ed.hpp"
|
#include "b64ed.hpp"
|
||||||
|
|
||||||
// Recursively decode and print if nested bb64 command is found
|
// 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 << std::string(80, '-') << std::endl;
|
||||||
std::cout << decoded << std::endl;
|
std::cout << decoded << std::endl;
|
||||||
|
std::cout << std::string(80, '-') << std::endl;
|
||||||
|
|
||||||
size_t pos = decoded.find("bb64 ");
|
size_t pos = decoded.find("bb64 ");
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
@ -34,27 +36,21 @@ constexpr unsigned int hash(const char *s, int off = 0)
|
|||||||
return !s[off] ? 5381 : (hash(s, off + 1) * 33) ^ s[off];
|
return !s[off] ? 5381 : (hash(s, off + 1) * 33) ^ s[off];
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int update_bb64()
|
||||||
{
|
{
|
||||||
if (argc < 2)
|
std::cout << "Updating bb64..." << std::endl;
|
||||||
{
|
|
||||||
std::cerr << "bb64 version " << VERSION << ", by J842." << std::endl;
|
|
||||||
// heredoc for instructions
|
|
||||||
std::cerr << R"(
|
|
||||||
|
|
||||||
Usage:
|
// determine path to this executable
|
||||||
bb64 BASE64COMMAND Decodes and runs the command
|
std::filesystem::path bb64_path = std::filesystem::canonical("/proc/self/exe");
|
||||||
bb64 -[i|d] BASE64COMMAND Displays the decoded command
|
std::cout << "bb64 path: " << bb64_path << std::endl;
|
||||||
bb64 -e COMMAND Encodes the command and prints the result
|
|
||||||
|
|
||||||
)" << std::endl;
|
return 0;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc == 2)
|
int decode_and_run(const std::string &encoded)
|
||||||
{
|
{
|
||||||
// Default: decode and run
|
// Default: decode and run
|
||||||
std::string decoded = base64_decode(argv[1]);
|
std::string decoded = base64_decode(encoded);
|
||||||
if (decoded.empty())
|
if (decoded.empty())
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to decode base64 command." << std::endl;
|
std::cerr << "Failed to decode base64 command." << std::endl;
|
||||||
@ -67,13 +63,42 @@ Usage:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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|d] BASE64COMMAND Displays the decoded command
|
||||||
|
bb64 -e COMMAND Encodes the command and prints the result
|
||||||
|
bb64 -u Updates bb64 to the latest version (may require sudo)
|
||||||
|
|
||||||
|
)" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
std::string mode = argv[1];
|
std::string mode = argv[1];
|
||||||
|
|
||||||
|
if (argc == 2)
|
||||||
|
{
|
||||||
|
if (mode == "-u")
|
||||||
|
return update_bb64();
|
||||||
|
else
|
||||||
|
return decode_and_run(mode);
|
||||||
|
}
|
||||||
|
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
|
||||||
switch (hash(mode.c_str()))
|
switch (hash(mode.c_str()))
|
||||||
{
|
{
|
||||||
case hash("-i"):
|
case hash("-i"):
|
||||||
case hash("-d"):
|
case hash("-d"):
|
||||||
|
std::cout << "Decoding command..." << std::endl
|
||||||
|
<< std::endl;
|
||||||
recursive_print(base64_decode(argv[2]));
|
recursive_print(base64_decode(argv[2]));
|
||||||
break;
|
break;
|
||||||
case hash("-e"):
|
case hash("-e"):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user