diff options
author | Mattias Andrée <maandree@operamail.com> | 2013-02-06 10:44:01 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2013-02-06 10:44:01 +0100 |
commit | efaf0e44161432f39eb145c32ecd2b6f94e67aae (patch) | |
tree | d9169636aebda77c7a42ff8433ab6f6add6330a3 /c/sha3sum.c | |
parent | beginning of porting the command to c (diff) | |
download | sha3sum-efaf0e44161432f39eb145c32ecd2b6f94e67aae.tar.gz sha3sum-efaf0e44161432f39eb145c32ecd2b6f94e67aae.tar.bz2 sha3sum-efaf0e44161432f39eb145c32ecd2b6f94e67aae.tar.xz |
minor work on port
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | c/sha3sum.c | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/c/sha3sum.c b/c/sha3sum.c index 026ab6d..a0ed1ec 100644 --- a/c/sha3sum.c +++ b/c/sha3sum.c @@ -286,10 +286,57 @@ int main(int argc, char** argv) continue; } fn = filename ? filename : "/dev/stdin"; + FILE* file = fopen(fn, "r"); + if (file == null) + { + fprintf(stderr, "%s: cannot read file: %s\n", cmd, filename); + fail = true; + continue; + } + initialise(r, c, o); + /* String rc = ""; */ - /* InputStream file = null; */ + int blksize = 4096; /** XXX os.stat(os.path.realpath(fn)).st_size; **/ + char* chunk = malloc(blksize) + for (;;) + { + long read = fread(chunk, 1, blksize, file); + if (read <= 0) + break; + update(chunk, read); + } + free(chunk); + byte* bs = digest(null, 0); + long bsn = (o + 7) >> 3; + for (long _ = 1; _ < i; _++) + { + initialise(r, c, o); + bs = digest(bs, bsn); + } + + if (binary) + { + if (filename == null) + stdin = bs; + for (long j = 0; j < bsn; j++) + putchar(*(bs + j)); + fflush(stdout); + } + else + { + for (int b = 0, bn = bs.length; b < bn; b++) + { rc += "0123456789ABCDEF".charAt((bs[b] >> 4) & 15); + rc += "0123456789ABCDEF".charAt(bs[b] & 15); + } + rc += " " + (filename == null ? "-" : filename) + "\n"; + if (filename == null) + stdin = rc.getBytes("UTF-8"); + System.out.print(rc); + fflush(stdout); + } - /* --------------------------------------------------------------- */ + free(bs); + fclose(file); } fflush(stdout); |