cksum.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibCore/File.h>
  9. #include <LibCrypto/Checksum/Adler32.h>
  10. #include <LibCrypto/Checksum/CRC32.h>
  11. #include <LibMain/Main.h>
  12. #include <string.h>
  13. ErrorOr<int> serenity_main(Main::Arguments arguments)
  14. {
  15. Vector<StringView> paths;
  16. StringView opt_algorithm;
  17. Core::ArgsParser args_parser;
  18. args_parser.add_option(opt_algorithm, "Checksum algorithm (default 'crc32', use 'list' to list available algorithms)", "algorithm", '\0', nullptr);
  19. args_parser.add_positional_argument(paths, "File", "file", Core::ArgsParser::Required::No);
  20. args_parser.parse(arguments);
  21. auto algorithm = opt_algorithm.is_empty() ? "crc32"sv : opt_algorithm;
  22. auto available_algorithms = Vector<StringView> { "crc32"sv, "adler32"sv };
  23. if (algorithm == "list") {
  24. outln("Available algorithms:");
  25. for (auto& available_algorithm : available_algorithms) {
  26. outln(available_algorithm);
  27. }
  28. exit(0);
  29. }
  30. if (!available_algorithms.contains_slow(algorithm)) {
  31. warnln("{}: Unknown checksum algorithm: {}", arguments.strings[0], algorithm);
  32. exit(1);
  33. }
  34. if (paths.is_empty())
  35. paths.append("-"sv);
  36. bool fail = false;
  37. Array<u8, PAGE_SIZE> buffer;
  38. for (auto& path : paths) {
  39. auto file_or_error = Core::File::open_file_or_standard_stream(path, Core::File::OpenMode::Read);
  40. auto filepath = (path == "-"sv) ? "/dev/stdin"sv : path;
  41. if (file_or_error.is_error()) {
  42. warnln("{}: {}: {}", arguments.strings[0], filepath, file_or_error.error());
  43. fail = true;
  44. continue;
  45. }
  46. auto file = file_or_error.release_value();
  47. size_t file_size = 0;
  48. if (algorithm == "crc32"sv) {
  49. Crypto::Checksum::CRC32 crc32;
  50. while (!file->is_eof()) {
  51. auto data_or_error = file->read_some(buffer);
  52. if (data_or_error.is_error()) {
  53. warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
  54. fail = true;
  55. continue;
  56. }
  57. file_size += data_or_error.value().size();
  58. crc32.update(data_or_error.value());
  59. }
  60. outln("{:08x} {} {}", crc32.digest(), file_size, path);
  61. } else if (algorithm == "adler32"sv) {
  62. Crypto::Checksum::Adler32 adler32;
  63. while (!file->is_eof()) {
  64. auto data_or_error = file->read_some(buffer);
  65. if (data_or_error.is_error()) {
  66. warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
  67. fail = true;
  68. continue;
  69. }
  70. file_size += data_or_error.value().size();
  71. adler32.update(data_or_error.value());
  72. }
  73. outln("{:08x} {} {}", adler32.digest(), file_size, path);
  74. } else {
  75. warnln("{}: Unknown checksum algorithm: {}", arguments.strings[0], algorithm);
  76. exit(1);
  77. }
  78. }
  79. return fail;
  80. }