From 0ac922535120596d5e03c0f263964f4af5242a37 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 20 Jun 2016 16:50:38 +1000 Subject: [PATCH] tools/hash: use new simple hash api --- tools/hash.cpp | 78 +++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/tools/hash.cpp b/tools/hash.cpp index e2a9cb5e..5f1961c5 100644 --- a/tools/hash.cpp +++ b/tools/hash.cpp @@ -23,6 +23,10 @@ #include #include +#include "io.hpp" + +#include "hash/simple.hpp" + #include "hash/adler.hpp" #include "hash/bsdsum.cpp" #include "hash/crc.hpp" @@ -51,44 +55,52 @@ const char* NAMES[] = { }; +/////////////////////////////////////////////////////////////////////////////// +std::ostream& +print_digest (std::ostream &os, uint32_t t) +{ + return os << std::hex << t << std::dec; +} + + +//----------------------------------------------------------------------------- +template +std::ostream& +print_digest (std::ostream &os, std::array digest) +{ + os << std::hex; + + for (auto c: digest) + os << +(c >> 4) << +(c & 0x0F); + + os << std::dec; + return os; +} + + /////////////////////////////////////////////////////////////////////////////// static void -compute (std::istream &is, const char *name) +compute (const std::string &name, + const unsigned char *restrict first, + const unsigned char *restrict last) { - std::vector data { - std::istream_iterator (is), - std::istream_iterator ( ) - }; - - #define simple(FUNC) do { \ - if (std::strcmp (name, #FUNC)) \ - break; \ - \ - std::cout << std::hex << FUNC (data.data (), data.size ()) << "\n"; \ - } while (0) - - simple (adler32); - simple (bsdsum); - simple (crc32); - - #undef simple - #define stream(TYPE) do { \ - if (std::strcmp (name, #TYPE)) \ + if (name != #TYPE) \ break; \ \ - util::hash::TYPE accum; \ - accum.update (data.data (), data.size ()); \ - accum.finish (); \ + auto sum = util::hash::simple ( \ + first, last \ + ); \ \ - std::cout << std::hex; \ - for (auto c: accum.digest ()) \ - std::cout << unsigned (c >> 4) << unsigned(c & 0x0F); \ - std::cout << std::dec << "\n"; \ + print_digest (std::cout, sum) << '\n'; \ return; \ } while (0); + stream (adler32); + stream (bsdsum); + stream (crc32); + stream (MD2); stream (MD4); stream (MD5); @@ -133,10 +145,12 @@ main (int argc, char **argv) } if (strcmp (argv[ARG_INPUT], "-")) { - std::cerr << "stdin only currently\n"; - return EXIT_FAILURE; - } + util::mapped_file src (argv[ARG_INPUT]); + compute (argv[ARG_HASH], src.cbegin (), src.cend ()); - compute (std::cin, argv[ARG_HASH]); - return EXIT_SUCCESS; + return EXIT_SUCCESS; + } else { + //compute (argv[ARG_HASH], std::cin); + return EXIT_SUCCESS; + } }