diff --git a/include/dindexer-machinery/tiger.hpp b/include/dindexer-machinery/tiger.hpp index 8d74daf..b8d540e 100644 --- a/include/dindexer-machinery/tiger.hpp +++ b/include/dindexer-machinery/tiger.hpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace mchlib { struct TigerHash { @@ -35,6 +36,8 @@ namespace mchlib { uint64_t data[3]; uint8_t byte_data[sizeof(uint64_t) * 3]; }; + + bool operator== (const TigerHash& parOther) const { return part_a == parOther.part_a and part_b == parOther.part_b and part_c == parOther.part_c; } }; static_assert(sizeof(TigerHash) == 24, "Wrong struct size"); @@ -42,6 +45,7 @@ namespace mchlib { void tiger_file ( const std::string& parPath, TigerHash& parHashFile, TigerHash& parHashDir, uint64_t& parSizeOut ); void tiger_init_hash ( TigerHash& parHash ); std::string tiger_to_string ( const TigerHash& parHash, bool parUpcase=false ); + TigerHash string_to_tiger ( const std::string& parString ); void tiger_data ( const std::string& parData, TigerHash& parHash ); void tiger_data ( const std::vector& parData, TigerHash& parHash ); } //namespace mchlib diff --git a/src/machinery/tiger.cpp b/src/machinery/tiger.cpp index 4531ccd..70fd62b 100644 --- a/src/machinery/tiger.cpp +++ b/src/machinery/tiger.cpp @@ -16,6 +16,7 @@ */ #include "dindexer-machinery/tiger.hpp" +#include "helpers/casts.hpp" #include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include extern "C" void tiger ( const char* parStr, uint64_t parLength, uint64_t parHash[3], char parPadding ); @@ -153,6 +155,20 @@ namespace mchlib { return oss.str(); } + TigerHash string_to_tiger (const std::string& parString) { + using boost::string_ref; + using TigerPartType = decltype(TigerHash::part_a); + + assert(parString.size() == sizeof(TigerHash) * 2); + + TigerHash retval; + const string_ref inp(parString); + retval.part_a = swap_long(dinhelp::lexical_cast(inp.substr(0, sizeof(TigerPartType) * 2))); + retval.part_b = swap_long(dinhelp::lexical_cast(inp.substr(sizeof(TigerPartType) * 2, sizeof(TigerPartType) * 2))); + retval.part_c = swap_long(dinhelp::lexical_cast(inp.substr(sizeof(TigerPartType) * 4, sizeof(TigerPartType) * 2))); + return retval; + } + void tiger_data (const std::string& parData, TigerHash& parHash) { tiger (parData.data(), parData.size(), parHash.data, g_tiger_padding); } diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index dfbe920..4a80dfe 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(${PROJECT_NAME} test_diriterator.cpp test_guess_content_type.cpp test_glob2regex.cpp + test_tiger_string_conv.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM diff --git a/test/unit/test_tiger_string_conv.cpp b/test/unit/test_tiger_string_conv.cpp new file mode 100644 index 0000000..2e04395 --- /dev/null +++ b/test/unit/test_tiger_string_conv.cpp @@ -0,0 +1,63 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#include "dindexer-machinery/tiger.hpp" +#include +#include + +TEST(machinery, TigerStringConv) { + using mchlib::TigerHash; + + const std::size_t TigerStringCharCount = sizeof(TigerHash) * 2; + { + TigerHash tiger_zero; + tiger_zero.part_a = tiger_zero.part_b = tiger_zero.part_c = 0; + + const std::string expected(TigerStringCharCount, '0'); + const std::string got = mchlib::tiger_to_string(tiger_zero); + EXPECT_EQ(expected, got); + + auto hash2 = mchlib::string_to_tiger(got); + EXPECT_EQ(tiger_zero, hash2); + } + + { + TigerHash t; + mchlib::tiger_init_hash(t); + + const std::string expected = "efcdab89674523011032547698badcfe87e1b2c3b4a596f0"; + EXPECT_EQ(TigerStringCharCount, expected.size()); + const std::string got = mchlib::tiger_to_string(t); + EXPECT_EQ(expected, got); + + auto hash2 = mchlib::string_to_tiger(got); + EXPECT_EQ(t, hash2); + } + + { + TigerHash t; + t.part_a = t.part_b = t.part_c = 0xababababababababULL; + + const std::string expected = "abababababababababababababababababababababababab"; + EXPECT_EQ(TigerStringCharCount, expected.size()); + const std::string got = mchlib::tiger_to_string(t); + EXPECT_EQ(expected, got); + + auto hash2 = mchlib::string_to_tiger(got); + EXPECT_EQ(t, hash2); + } +}