serenity/Userland/Utilities/base64.cpp

40 lines
1 KiB
C++
Raw Normal View History

2020-06-12 22:55:35 -04:00
/*
* Copyright (c) 2020, Tom Lebreux <tomlebreux@hotmail.com>
*
* 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 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
{
TRY(Core::System::pledge("stdio rpath"));
2020-06-12 22:55:35 -04:00
bool decode = false;
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
auto file = TRY(Core::File::open_file_or_standard_stream(filepath, Core::File::OpenMode::Read));
ByteBuffer buffer = TRY(file->read_until_eof());
2020-06-12 22:55:35 -04: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;
}
auto encoded = TRY(encode_base64(buffer));
outln("{}", encoded);
2021-11-23 11:15:35 -05:00
return 0;
2020-06-12 22:55:35 -04:00
}