2020-06-12 22:55:35 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Tom Lebreux <tomlebreux@hotmail.com>
|
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-12 22:55:35 -04:00
|
|
|
*/
|
|
|
|
|
2021-05-15 06:34:40 -04:00
|
|
|
#include <AK/Assertions.h>
|
2020-06-12 22:55:35 -04:00
|
|
|
#include <AK/Base64.h>
|
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2021-03-12 11:29:37 -05:00
|
|
|
#include <unistd.h>
|
2020-06-12 22:55:35 -04:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (pledge("stdio rpath", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool decode = false;
|
|
|
|
const char* filepath = nullptr;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(decode, "Decode data", "decode", 'd');
|
|
|
|
args_parser.add_positional_argument(filepath, "", "file", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
|
|
|
ByteBuffer buffer;
|
|
|
|
if (filepath == nullptr || strcmp(filepath, "-") == 0) {
|
|
|
|
auto file = Core::File::construct();
|
|
|
|
bool success = file->open(
|
|
|
|
STDIN_FILENO,
|
2021-05-12 05:26:43 -04:00
|
|
|
Core::OpenMode::ReadOnly,
|
2020-10-25 07:31:27 -04:00
|
|
|
Core::File::ShouldCloseFileDescriptor::Yes);
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(success);
|
2020-06-12 22:55:35 -04:00
|
|
|
buffer = file->read_all();
|
|
|
|
} else {
|
2021-05-12 05:26:43 -04:00
|
|
|
auto result = Core::File::open(filepath, Core::OpenMode::ReadOnly);
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(!result.is_error());
|
2020-06-12 22:55:35 -04:00
|
|
|
auto file = result.value();
|
|
|
|
buffer = file->read_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pledge("stdio", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decode) {
|
|
|
|
auto decoded = decode_base64(StringView(buffer));
|
2021-10-23 09:43:59 -04:00
|
|
|
if (!decoded.has_value()) {
|
|
|
|
warnln("base64: invalid input");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fwrite(decoded.value().data(), sizeof(u8), decoded.value().size(), stdout);
|
2020-06-12 22:55:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:38:24 -04:00
|
|
|
auto encoded = encode_base64(buffer);
|
2021-05-31 10:43:25 -04:00
|
|
|
outln("{}", encoded);
|
2020-06-12 22:55:35 -04:00
|
|
|
}
|