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