Add support for hashing data from stdin

This commit is contained in:
King_DuckZ 2021-05-28 23:08:19 +02:00
parent 172ba6811c
commit 7a40f9d31c
3 changed files with 26 additions and 20 deletions

View file

@ -23,9 +23,6 @@ import std.getopt;
import sums; import sums;
int main (string[] parArgs) { int main (string[] parArgs) {
if (parArgs.length == 1)
return 0;
const auto cwd = getcwd(); const auto cwd = getcwd();
const auto working_dir = cwd ~ (cwd[$-1] == '/' ? "" : "/"); const auto working_dir = cwd ~ (cwd[$-1] == '/' ? "" : "/");

View file

@ -26,6 +26,8 @@ import std.format;
import std.array : appender; import std.array : appender;
import std.path; import std.path;
private const string g_out_format = "%s %s";
private void writeError (string parFile, string parMessage) { private void writeError (string parFile, string parMessage) {
stderr.writefln("tigersum: %s: %s", parFile, parMessage); stderr.writefln("tigersum: %s: %s", parFile, parMessage);
} }
@ -34,7 +36,7 @@ private void writeWarning (string parMessage) {
writeError("WARNING", 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]; ulong[] read_buff = new ulong[READ_BUFF_BLOCKS * 64 / ulong.sizeof];
foreach (file_name; parFiles) { 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); writefln("tiger (%s) = %s", file_name, hash);
} }
else { else {
writefln("%s %s", hash, file_name); writefln(g_out_format, hash, file_name);
} }
} }
else { 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) { bool checkSums (in string parFile, in string parRoot, bool parTiger2, string parFileMode) {
File curr_list = File(parFile, "r"); File curr_list = File(parFile, "r");
bool ret_val = true; bool ret_val = true;

View file

@ -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); assert((cast(size_t)parBuffer.ptr & cast(size_t)(ulong.alignof - 1)) == 0);
ulong[3] retval; ulong[3] retval;
auto data_left = parData.size; char[] data, prev_data;
const auto buff_length = 64 * READ_BUFF_BLOCKS; ulong real_length = 0;
tiger_init(retval); tiger_init(retval);
while (data_left >= buff_length) { while (!(data = parData.rawRead(parBuffer)).empty) {
parData.rawRead(parBuffer); tiger_chunk(data.ptr, data.length, retval);
tiger_chunk(parBuffer.ptr, buff_length, retval); real_length += data.length;
data_left -= buff_length; prev_data = data;
} }
const char padding = (parV2 ? 0x80 : 0x01); const char padding = (parV2 ? 0x80 : 0x01);
if (data_left) { const ulong proc_length = prev_data.length & ~0x3fUL;
parData.rawRead(parBuffer[0 .. data_left]); tiger_last_chunk(prev_data.ptr + proc_length, prev_data.length - proc_length, real_length, retval, padding);
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);
}
return retval; return retval;
} }