BuildCache
hasher.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2018 Marcus Geelnard
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_HASHER_HPP_
21 #define BUILDCACHE_HASHER_HPP_
22 
23 #include <base/string_list.hpp>
24 
25 #include <cstdint>
26 
27 #include <map>
28 #include <string>
29 
30 namespace bcache {
31 
33 class hasher_t {
34 public:
36  class hash_t {
37  public:
38  uint8_t* data() {
39  return &m_data[0];
40  }
41 
42  const uint8_t* data() const {
43  return &m_data[0];
44  }
45 
48  std::string as_string() const;
49 
50  // The hash size is 128 bits.
51  static const size_t SIZE = 16U;
52 
53  private:
54  uint8_t m_data[SIZE]{};
55  };
56 
57  hasher_t();
58  ~hasher_t();
59 
62  hasher_t(const hasher_t& other);
63 
66  hasher_t& operator=(const hasher_t& other);
67 
71  void update(const void* data, const size_t size);
72 
75  void update(const std::string& text) {
76  update(text.data(), text.size());
77  }
78 
81  void update(const string_list_t& data);
82 
85  void update(const std::map<std::string, std::string>& data);
86 
90  void update_from_file(const std::string& path);
91 
98  void update_from_file_deterministic(const std::string& path);
99 
104  void inject_separator();
105 
109  hash_t final();
110 
111 private:
114  void update_from_ar_data(const std::string& data);
115 
116  // This is in fact an XXH3_state_t pointer, but we don't want to include xxhash.h in this
117  // header (to avoid leaking inlined code and to avoid namespace pollution).
118  void* m_state = nullptr;
119 };
120 
121 } // namespace bcache
122 
123 #endif // BUILDCACHE_HASHER_HPP_
bcache::hasher_t::operator=
hasher_t & operator=(const hasher_t &other)
Copy the hash state to an existing hasher_t object.
Definition: hasher.cpp:82
bcache::hasher_t::update_from_file_deterministic
void update_from_file_deterministic(const std::string &path)
Update the hash with more data.
Definition: hasher.cpp:120
bcache::hasher_t::hash_t
A helper class for storing the data hash.
Definition: hasher.hpp:36
bcache::hasher_t::update
void update(const std::string &text)
Update the hash with more data.
Definition: hasher.hpp:75
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::string_list_t
A convenient vector of strings container with support functions for program argument handling.
Definition: string_list.hpp:30
bcache::hasher_t
A class for hashing data.
Definition: hasher.hpp:33
bcache::hasher_t::update
void update(const void *data, const size_t size)
Update the hash with more data.
Definition: hasher.cpp:88
bcache::hasher_t::hash_t::as_string
std::string as_string() const
Convert a hash to a hexadecimal string.
Definition: hasher.cpp:47
bcache::hasher_t::inject_separator
void inject_separator()
Update the hash with a separator sequence.
Definition: hasher.cpp:130
bcache::hasher_t::update_from_file
void update_from_file(const std::string &path)
Update the hash with more data.
Definition: hasher.cpp:112
bcache::hasher_t::update_from_ar_data
void update_from_ar_data(const std::string &data)
Update the hash with data from an AR archive.
Definition: hasher.cpp:134