2021-04-24 12:46:30 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-03-10 18:27:00 +03:00
|
|
|
#include <AK/String.h>
|
2021-04-24 12:46:30 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-04-24 12:46:30 +03:00
|
|
|
#include <LibCrypto/Checksum/Adler32.h>
|
|
|
|
#include <LibCrypto/Checksum/CRC32.h>
|
2021-11-27 18:51:46 +03:00
|
|
|
#include <LibMain/Main.h>
|
2021-11-07 04:15:10 +03:00
|
|
|
#include <string.h>
|
2021-04-24 12:46:30 +03:00
|
|
|
|
2021-11-27 18:51:46 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-04-24 12:46:30 +03:00
|
|
|
{
|
2023-03-10 18:27:00 +03:00
|
|
|
Vector<StringView> paths;
|
2023-02-28 23:41:43 +03:00
|
|
|
StringView opt_algorithm;
|
2021-04-24 12:46:30 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(opt_algorithm, "Checksum algorithm (default 'crc32', use 'list' to list available algorithms)", "algorithm", '\0', nullptr);
|
|
|
|
args_parser.add_positional_argument(paths, "File", "file", Core::ArgsParser::Required::No);
|
2021-11-27 18:51:46 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-04-24 12:46:30 +03:00
|
|
|
|
2023-03-10 18:27:00 +03:00
|
|
|
auto algorithm = opt_algorithm.is_empty() ? "crc32"sv : opt_algorithm;
|
2021-04-24 12:46:30 +03:00
|
|
|
|
2023-03-10 18:27:00 +03:00
|
|
|
auto available_algorithms = Vector<StringView> { "crc32"sv, "adler32"sv };
|
2021-04-24 12:46:30 +03:00
|
|
|
|
|
|
|
if (algorithm == "list") {
|
|
|
|
outln("Available algorithms:");
|
|
|
|
for (auto& available_algorithm : available_algorithms) {
|
|
|
|
outln(available_algorithm);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!available_algorithms.contains_slow(algorithm)) {
|
2021-11-27 18:51:46 +03:00
|
|
|
warnln("{}: Unknown checksum algorithm: {}", arguments.strings[0], algorithm);
|
2021-04-24 12:46:30 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paths.is_empty())
|
2023-03-10 18:27:00 +03:00
|
|
|
paths.append("-"sv);
|
2021-04-24 12:46:30 +03:00
|
|
|
|
|
|
|
bool fail = false;
|
2022-09-20 13:58:46 +03:00
|
|
|
Array<u8, PAGE_SIZE> buffer;
|
|
|
|
|
2021-04-24 12:46:30 +03:00
|
|
|
for (auto& path : paths) {
|
2023-02-09 05:02:46 +03:00
|
|
|
auto file_or_error = Core::File::open_file_or_standard_stream(path, Core::File::OpenMode::Read);
|
2023-03-10 18:27:00 +03:00
|
|
|
auto filepath = (path == "-"sv) ? "/dev/stdin"sv : path;
|
2022-09-20 13:58:46 +03:00
|
|
|
if (file_or_error.is_error()) {
|
|
|
|
warnln("{}: {}: {}", arguments.strings[0], filepath, file_or_error.error());
|
2021-04-24 12:46:30 +03:00
|
|
|
fail = true;
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-20 13:58:46 +03:00
|
|
|
auto file = file_or_error.release_value();
|
2022-10-12 16:46:15 +03:00
|
|
|
size_t file_size = 0;
|
2022-09-20 13:58:46 +03:00
|
|
|
|
2023-03-10 18:27:00 +03:00
|
|
|
if (algorithm == "crc32"sv) {
|
2021-07-31 20:01:47 +03:00
|
|
|
Crypto::Checksum::CRC32 crc32;
|
2022-09-20 13:58:46 +03:00
|
|
|
while (!file->is_eof()) {
|
2023-02-25 00:38:01 +03:00
|
|
|
auto data_or_error = file->read_some(buffer);
|
2022-09-20 13:58:46 +03:00
|
|
|
if (data_or_error.is_error()) {
|
|
|
|
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
|
|
|
|
fail = true;
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-12 16:46:15 +03:00
|
|
|
file_size += data_or_error.value().size();
|
2022-09-20 13:58:46 +03:00
|
|
|
crc32.update(data_or_error.value());
|
2021-07-31 20:01:47 +03:00
|
|
|
}
|
2022-10-12 16:46:15 +03:00
|
|
|
outln("{:08x} {} {}", crc32.digest(), file_size, path);
|
2023-03-10 18:27:00 +03:00
|
|
|
} else if (algorithm == "adler32"sv) {
|
2021-07-31 20:01:47 +03:00
|
|
|
Crypto::Checksum::Adler32 adler32;
|
2022-09-20 13:58:46 +03:00
|
|
|
while (!file->is_eof()) {
|
2023-02-25 00:38:01 +03:00
|
|
|
auto data_or_error = file->read_some(buffer);
|
2022-09-20 13:58:46 +03:00
|
|
|
if (data_or_error.is_error()) {
|
|
|
|
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
|
|
|
|
fail = true;
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-12 16:46:15 +03:00
|
|
|
file_size += data_or_error.value().size();
|
2022-09-20 13:58:46 +03:00
|
|
|
adler32.update(data_or_error.value());
|
2021-07-31 20:01:47 +03:00
|
|
|
}
|
2022-10-12 16:46:15 +03:00
|
|
|
outln("{:08x} {} {}", adler32.digest(), file_size, path);
|
2021-04-24 12:46:30 +03:00
|
|
|
} else {
|
2021-11-27 18:51:46 +03:00
|
|
|
warnln("{}: Unknown checksum algorithm: {}", arguments.strings[0], algorithm);
|
2021-04-24 12:46:30 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fail;
|
|
|
|
}
|