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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Base64.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-08 21:02:46 -05:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 11:15:35 -05:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-06-12 22:55:35 -04:00
|
|
|
|
2021-11-23 11:15:35 -05:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-06-12 22:55:35 -04:00
|
|
|
{
|
2021-11-27 17:26:34 -05:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-06-12 22:55:35 -04:00
|
|
|
|
|
|
|
bool decode = false;
|
2022-04-30 15:50:21 -04:00
|
|
|
StringView filepath = {};
|
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 11:15:35 -05:00
|
|
|
args_parser.parse(arguments);
|
2020-06-12 22:55:35 -04:00
|
|
|
|
2023-02-08 21:02:46 -05:00
|
|
|
auto file = TRY(Core::File::open_file_or_standard_stream(filepath, Core::File::OpenMode::Read));
|
2022-12-11 11:49:00 -05:00
|
|
|
ByteBuffer buffer = TRY(file->read_until_eof());
|
2020-06-12 22:55:35 -04:00
|
|
|
|
2021-11-27 17:26:34 -05:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-06-12 22:55:35 -04:00
|
|
|
|
|
|
|
if (decode) {
|
2022-09-13 09:46:49 -04:00
|
|
|
auto decoded = TRY(decode_base64(buffer));
|
|
|
|
out("{}", StringView(decoded.bytes()));
|
2020-06-12 22:55:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-08 11:33:11 -05:00
|
|
|
auto encoded = TRY(encode_base64(buffer));
|
2021-05-31 10:43:25 -04:00
|
|
|
outln("{}", encoded);
|
2021-11-23 11:15:35 -05:00
|
|
|
return 0;
|
2020-06-12 22:55:35 -04:00
|
|
|
}
|