2020-06-12 23:55:35 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Tom Lebreux <tomlebreux@hotmail.com>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-12 23:55:35 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Base64.h>
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
|
#include <LibCore/File.h>
|
2021-11-23 13:15:35 -03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2020-06-12 23:55:35 -03:00
|
|
|
|
2021-11-23 13:15:35 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-06-12 23:55:35 -03:00
|
|
|
{
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-06-12 23:55:35 -03:00
|
|
|
|
|
|
|
|
bool decode = false;
|
2022-04-30 16:50:21 -03:00
|
|
|
StringView filepath = {};
|
2020-06-12 23:55:35 -03:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
args_parser.add_option(decode, "Decode data", "decode", 'd');
|
|
|
|
|
args_parser.add_positional_argument(filepath, "", "file", Core::ArgsParser::Required::No);
|
2021-11-23 13:15:35 -03:00
|
|
|
args_parser.parse(arguments);
|
2020-06-12 23:55:35 -03:00
|
|
|
|
|
|
|
|
ByteBuffer buffer;
|
2022-04-30 16:50:21 -03:00
|
|
|
if (filepath == nullptr || filepath == "-") {
|
2022-04-30 16:13:17 -03:00
|
|
|
buffer = Core::File::standard_input()->read_all();
|
2020-06-12 23:55:35 -03:00
|
|
|
} else {
|
2022-04-30 16:14:13 -03:00
|
|
|
auto file = TRY(Core::File::open(filepath, Core::OpenMode::ReadOnly));
|
2020-06-12 23:55:35 -03:00
|
|
|
buffer = file->read_all();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-06-12 23:55:35 -03:00
|
|
|
|
|
|
|
|
if (decode) {
|
2022-01-20 14:18:17 -03:00
|
|
|
auto decoded = TRY(decode_base64(StringView(buffer)));
|
2022-04-30 16:13:17 -03:00
|
|
|
Core::File::standard_output()->write(decoded.data(), decoded.size());
|
2020-06-12 23:55:35 -03:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-15 13:38:24 -03:00
|
|
|
auto encoded = encode_base64(buffer);
|
2021-05-31 11:43:25 -03:00
|
|
|
outln("{}", encoded);
|
2021-11-23 13:15:35 -03:00
|
|
|
return 0;
|
2020-06-12 23:55:35 -03:00
|
|
|
}
|