From 7a40f9d31c7cedb876dff99765caf14e10b3c74f Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 28 May 2021 23:08:19 +0200 Subject: [PATCH] Add support for hashing data from stdin --- src/main.d | 3 --- src/sums.d | 20 ++++++++++++++++++-- src/tiger.d | 23 ++++++++--------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main.d b/src/main.d index 4b54157..2eddf43 100644 --- a/src/main.d +++ b/src/main.d @@ -23,9 +23,6 @@ import std.getopt; import sums; int main (string[] parArgs) { - if (parArgs.length == 1) - return 0; - const auto cwd = getcwd(); const auto working_dir = cwd ~ (cwd[$-1] == '/' ? "" : "/"); diff --git a/src/sums.d b/src/sums.d index d64d29e..3284644 100644 --- a/src/sums.d +++ b/src/sums.d @@ -26,6 +26,8 @@ import std.format; import std.array : appender; import std.path; +private const string g_out_format = "%s %s"; + private void writeError (string parFile, string parMessage) { stderr.writefln("tigersum: %s: %s", parFile, parMessage); } @@ -34,7 +36,7 @@ private void writeWarning (string parMessage) { writeError("WARNING", parMessage); } -void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) { +private void calculateSumsOnFiles (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) { ulong[] read_buff = new ulong[READ_BUFF_BLOCKS * 64 / ulong.sizeof]; foreach (file_name; parFiles) { @@ -54,7 +56,7 @@ void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, stri writefln("tiger (%s) = %s", file_name, hash); } else { - writefln("%s %s", hash, file_name); + writefln(g_out_format, hash, file_name); } } else { @@ -63,6 +65,20 @@ void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, stri } } +private void calculateSumsOnStdIn (bool parTiger2, bool parBsdStyle) { + const string hash = tigerToString(getTiger(stdin, parTiger2)); + writefln(g_out_format, hash, "-"); +} + +void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) { + if (parFiles.empty || parFiles.length == 1 && parFiles[0] == "-") { + calculateSumsOnStdIn(parTiger2, parBsdStyle); + } + else { + calculateSumsOnFiles(parFiles, parCWD, parTiger2, parFileMode, parBsdStyle); + } +} + bool checkSums (in string parFile, in string parRoot, bool parTiger2, string parFileMode) { File curr_list = File(parFile, "r"); bool ret_val = true; diff --git a/src/tiger.d b/src/tiger.d index 13806cf..4cc9923 100644 --- a/src/tiger.d +++ b/src/tiger.d @@ -53,26 +53,19 @@ public ulong[3] getTiger (ref File parData, bool parV2, char[] parBuffer) { assert((cast(size_t)parBuffer.ptr & cast(size_t)(ulong.alignof - 1)) == 0); ulong[3] retval; - auto data_left = parData.size; - const auto buff_length = 64 * READ_BUFF_BLOCKS; + char[] data, prev_data; + ulong real_length = 0; tiger_init(retval); - while (data_left >= buff_length) { - parData.rawRead(parBuffer); - tiger_chunk(parBuffer.ptr, buff_length, retval); - data_left -= buff_length; + while (!(data = parData.rawRead(parBuffer)).empty) { + tiger_chunk(data.ptr, data.length, retval); + real_length += data.length; + prev_data = data; } const char padding = (parV2 ? 0x80 : 0x01); - if (data_left) { - parData.rawRead(parBuffer[0 .. data_left]); - const ulong proc_length = data_left & ~0x3fUL; - tiger_chunk(parBuffer.ptr, proc_length, retval); - tiger_last_chunk(parBuffer.ptr + proc_length, data_left - proc_length, parData.size, retval, padding); - } - else { - tiger_last_chunk(null, 0, parData.size, retval, padding); - } + const ulong proc_length = prev_data.length & ~0x3fUL; + tiger_last_chunk(prev_data.ptr + proc_length, prev_data.length - proc_length, real_length, retval, padding); return retval; }