1 // license:BSD-3-Clause 2 // copyright-holders:Aaron Giles 3 /*************************************************************************** 4 5 hash.h 6 7 Function to handle hash functions (checksums) 8 9 Based on original idea by Farfetch'd 10 11 ***************************************************************************/ 12 13 #ifndef MAME_UTIL_HASH_H 14 #define MAME_UTIL_HASH_H 15 16 #pragma once 17 18 #include "hashing.h" 19 20 21 //************************************************************************** 22 // MACROS 23 //************************************************************************** 24 25 // use these to define compile-time internal-format hash strings 26 #define CRC(x) "R" #x 27 #define SHA1(x) "S" #x 28 #define NO_DUMP "!" 29 #define BAD_DUMP "^" 30 31 32 namespace util { 33 //************************************************************************** 34 // TYPE DEFINITIONS 35 //************************************************************************** 36 37 38 // ======================> hash_collection 39 40 // a collection of the various supported hashes and flags 41 class hash_collection 42 { 43 public: 44 // hash types are identified by non-hex alpha values (G-Z) 45 static constexpr char HASH_CRC = 'R'; 46 static constexpr char HASH_SHA1 = 'S'; 47 48 // common combinations for requests 49 static char const *const HASH_TYPES_CRC; 50 static char const *const HASH_TYPES_CRC_SHA1; 51 static char const *const HASH_TYPES_ALL; 52 53 // flags are identified by punctuation marks 54 static constexpr char FLAG_NO_DUMP = '!'; 55 static constexpr char FLAG_BAD_DUMP = '^'; 56 57 // construction/destruction 58 hash_collection(); 59 hash_collection(const char *string); 60 hash_collection(const hash_collection &src); 61 ~hash_collection(); 62 63 // operators 64 hash_collection &operator=(const hash_collection &src); 65 bool operator==(const hash_collection &rhs) const; 66 bool operator!=(const hash_collection &rhs) const { return !(*this == rhs); } 67 68 // getters flag(char flag)69 bool flag(char flag) const { return (m_flags.find_first_of(flag) != std::string::npos); } 70 std::string hash_types() const; 71 72 // hash manipulators 73 void reset(); 74 bool add_from_string(char type, const char *buffer, int length = -1); 75 bool remove(char type); 76 77 // CRC-specific helpers crc(uint32_t & result)78 bool crc(uint32_t &result) const { result = m_crc32; return m_has_crc32; } add_crc(uint32_t crc)79 void add_crc(uint32_t crc) { m_crc32 = crc; m_has_crc32 = true; } 80 81 // SHA1-specific helpers sha1(sha1_t & result)82 bool sha1(sha1_t &result) const { result = m_sha1; return m_has_sha1; } add_sha1(sha1_t sha1)83 void add_sha1(sha1_t sha1) { m_has_sha1 = true; m_sha1 = sha1; } 84 85 // string conversion 86 std::string internal_string() const; 87 std::string macro_string() const; 88 std::string attribute_string() const; 89 bool from_internal_string(const char *string); 90 91 // creation 92 void begin(const char *types = nullptr); 93 void buffer(const uint8_t *data, uint32_t length); 94 void end(); 95 void compute(const uint8_t *data, uint32_t length, const char *types = nullptr) { begin(types); buffer(data, length); end(); } 96 97 private: 98 // internal helpers 99 void copyfrom(const hash_collection &src); 100 101 // internal state 102 std::string m_flags; 103 bool m_has_crc32; 104 crc32_t m_crc32; 105 bool m_has_sha1; 106 sha1_t m_sha1; 107 108 // creators 109 struct hash_creator 110 { 111 bool m_doing_crc32; 112 crc32_creator m_crc32_creator; 113 bool m_doing_sha1; 114 sha1_creator m_sha1_creator; 115 }; 116 hash_creator * m_creator; 117 }; 118 119 120 } // namespace util 121 122 #endif // MAME_UTIL_HASH_H 123