Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:29

0001 #include "DataFormats/Provenance/interface/CompactHash.h"
0002 #include "FWCore/Utilities/interface/Algorithms.h"
0003 #include "FWCore/Utilities/interface/Digest.h"
0004 #include "FWCore/Utilities/interface/EDMException.h"
0005 
0006 #include <functional>
0007 #include <cassert>
0008 
0009 namespace {
0010   std::array<unsigned char, 16> convert(std::string const& v) {
0011     assert(v.size() == 16);
0012     std::array<unsigned char, 16> retValue;
0013     std::copy(v.begin(), v.end(), retValue.begin());
0014     return retValue;
0015   }
0016 }  // namespace
0017 namespace edm {
0018   namespace detail {
0019     // This string is the 16-byte, non-printable version.
0020     std::array<unsigned char, 16> const& InvalidCompactHash() {
0021       static std::array<unsigned char, 16> const invalid = convert(cms::MD5Result().compactForm());
0022       return invalid;
0023     }
0024   }  // namespace detail
0025 
0026   namespace compact_hash_detail {
0027     size_t smallHash_(value_type const& hash) {
0028       //NOTE: In future we could try to xor the first 8bytes into the second 8bytes of the string to make the hash
0029       std::hash<std::string_view> h;
0030       return h(std::string_view(reinterpret_cast<const char*>(hash.data()), hash.size()));
0031     }
0032 
0033     std::array<unsigned char, 16> fromHex_(std::string_view v) {
0034       cms::MD5Result temp;
0035       temp.fromHexifiedString(v);
0036       auto hash = temp.compactForm();
0037       std::array<unsigned char, 16> ret;
0038       std::copy(hash.begin(), hash.end(), ret.begin());
0039       return ret;
0040     }
0041 
0042     bool isValid_(value_type const& hash) { return hash != detail::InvalidCompactHash(); }
0043 
0044     void throwIfIllFormed(std::string_view v) {
0045       // Fixup not needed here.
0046       if (v.size() != 16) {
0047         throw Exception(errors::LogicError) << "Ill-formed CompactHash instance. "
0048                                             << "A string_view of size " << v.size() << " passed to constructor.";
0049       }
0050     }
0051 
0052     void toString_(std::string& result, value_type const& hash) {
0053       cms::MD5Result temp;
0054       copy_all(hash, temp.bytes.begin());
0055       result += temp.toString();
0056     }
0057 
0058     void toDigest_(cms::Digest& digest, value_type const& hash) {
0059       cms::MD5Result temp;
0060       copy_all(hash, temp.bytes.begin());
0061       digest.append(temp.toString());
0062     }
0063 
0064     std::ostream& print_(std::ostream& os, value_type const& hash) {
0065       cms::MD5Result temp;
0066       copy_all(hash, temp.bytes.begin());
0067       os << temp.toString();
0068       return os;
0069     }
0070   }  // namespace compact_hash_detail
0071 }  // namespace edm