summaryrefslogtreecommitdiff
path: root/Userland/Utilities/cksum.cpp
blob: 87807a94a79c1c774f46f012f192ea52fd9121f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCrypto/Checksum/Adler32.h>
#include <LibCrypto/Checksum/CRC32.h>
#include <LibMain/Main.h>
#include <string.h>

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    Vector<const char*> paths;
    const char* opt_algorithm = nullptr;

    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);
    args_parser.parse(arguments);

    auto algorithm = (opt_algorithm == nullptr) ? "crc32" : String(opt_algorithm).to_lowercase();

    auto available_algorithms = Vector<String> { "crc32", "adler32" };

    if (algorithm == "list") {
        outln("Available algorithms:");
        for (auto& available_algorithm : available_algorithms) {
            outln(available_algorithm);
        }
        exit(0);
    }

    if (!available_algorithms.contains_slow(algorithm)) {
        warnln("{}: Unknown checksum algorithm: {}", arguments.strings[0], algorithm);
        exit(1);
    }

    if (paths.is_empty())
        paths.append("-");

    bool fail = false;
    for (auto& path : paths) {
        auto filepath = (StringView(path) == "-") ? "/dev/stdin" : path;
        auto file = Core::File::construct(filepath);
        if (!file->open(Core::OpenMode::ReadOnly)) {
            warnln("{}: {}: {}", arguments.strings[0], path, file->error_string());
            fail = true;
            continue;
        }
        struct stat st;
        if (fstat(file->fd(), &st) < 0) {
            warnln("{}: Failed to fstat {}: {}", arguments.strings[0], filepath, strerror(errno));
            fail = true;
            continue;
        }
        if (algorithm == "crc32") {
            Crypto::Checksum::CRC32 crc32;
            while (!file->eof() && !file->has_error())
                crc32.update(file->read(PAGE_SIZE));
            if (file->has_error()) {
                warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, file->error_string());
                fail = true;
                continue;
            }
            outln("{:08x} {} {}", crc32.digest(), st.st_size, path);
        } else if (algorithm == "adler32") {
            Crypto::Checksum::Adler32 adler32;
            while (!file->eof() && !file->has_error())
                adler32.update(file->read(PAGE_SIZE));
            if (file->has_error()) {
                warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, file->error_string());
                fail = true;
                continue;
            }
            outln("{:08x} {} {}", adler32.digest(), st.st_size, path);
        } else {
            warnln("{}: Unknown checksum algorithm: {}", arguments.strings[0], algorithm);
            exit(1);
        }
    }

    return fail;
}