Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:51:08

0001 #include "DataFormats/Provenance/interface/Hash.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 
0008 namespace edm {
0009   namespace detail {
0010     // This string is the 16-byte, non-printable version.
0011     std::string const& InvalidHash() {
0012       static std::string const invalid = cms::MD5Result().compactForm();
0013       return invalid;
0014     }
0015   }  // namespace detail
0016 
0017   namespace hash_detail {
0018     value_type compactForm_(value_type const& hash) {
0019       if (isCompactForm_(hash)) {
0020         return hash;
0021       }
0022       value_type temp(hash);
0023       fixup_(temp);
0024       return temp;
0025     }
0026 
0027     // 'Fix' the string data member of this Hash, i.e., if it is in
0028     // the hexified (32 byte) representation, make it be in the
0029     // 16-byte (unhexified) representation.
0030     void fixup_(value_type& hash) {
0031       switch (hash.size()) {
0032         case 16: {
0033           break;
0034         }
0035         case 32: {
0036           cms::MD5Result temp;
0037           temp.fromHexifiedString(hash);
0038           hash = temp.compactForm();
0039           break;
0040         }
0041         case 0: {
0042           throw Exception(errors::LogicError) << "Empty edm::Hash<> instance:\n"
0043                                               << "\nPlease report this to the core framework developers";
0044         }
0045         default: {
0046           throw Exception(errors::LogicError) << "edm::Hash<> instance with data in illegal state:\n"
0047                                               << hash << "\nPlease report this to the core framework developers";
0048         }
0049       }
0050     }
0051 
0052     size_t smallHash_(value_type const& hash) {
0053       //NOTE: In future we could try to xor the first 8bytes into the second 8bytes of the string to make the hash
0054       std::hash<std::string> h;
0055       if (hash.size() == 16) {
0056         return h(hash);
0057       }
0058       return h(compactForm_(hash));
0059     }
0060 
0061     bool isCompactForm_(value_type const& hash) { return 16 == hash.size(); }
0062 
0063     bool isValid_(value_type const& hash) {
0064       return isCompactForm_(hash) ? (hash != detail::InvalidHash()) : (!hash.empty());
0065     }
0066 
0067     void throwIfIllFormed(value_type const& hash) {
0068       // Fixup not needed here.
0069       if (hash.size() % 2 == 1) {
0070         throw Exception(errors::LogicError) << "Ill-formed Hash instance. "
0071                                             << "Please report this to the core framework developers";
0072       }
0073     }
0074 
0075     void toString_(std::string& result, value_type const& hash) {
0076       value_type temp1(hash);
0077       fixup_(temp1);
0078       cms::MD5Result temp;
0079       copy_all(temp1, temp.bytes.begin());
0080       result += temp.toString();
0081     }
0082 
0083     void toDigest_(cms::Digest& digest, value_type const& hash) {
0084       // FIXME: do we really need to go through a temporary value_type???
0085       value_type temp1(hash);
0086       fixup_(temp1);
0087       cms::MD5Result temp;
0088       copy_all(temp1, temp.bytes.begin());
0089       digest.append(temp.toString());
0090     }
0091 
0092     std::ostream& print_(std::ostream& os, value_type const& hash) {
0093       value_type temp1(hash);
0094       fixup_(temp1);
0095       cms::MD5Result temp;
0096       copy_all(temp1, temp.bytes.begin());
0097       os << temp.toString();
0098       return os;
0099     }
0100   }  // namespace hash_detail
0101 }  // namespace edm