LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
hash.hpp
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_HASH_H
17#define LIEF_HASH_H
18
19#include <ostream>
20#include <vector>
21
22#include "LIEF/visibility.h"
23#include "LIEF/Object.hpp"
24#include "LIEF/Visitor.hpp"
25#include "LIEF/span.hpp"
26
27
28namespace LIEF {
29
30
31class LIEF_API Hash : public Visitor {
32 public:
33 using value_type = size_t;
34 template<class H = Hash>
35 static value_type hash(const Object& obj);
36
37 static value_type hash(const std::vector<uint8_t>& raw);
38 static value_type hash(span<const uint8_t> raw);
39 static value_type hash(const void* raw, size_t size);
40
41 // combine two elements to produce a size_t.
42 template<typename U = value_type>
43 static value_type combine(value_type lhs, U rhs) {
44 return (lhs ^ rhs) + 0x9e3779b9 + (lhs << 6) + (rhs >> 2);
45 }
46
47 public:
48 using Visitor::visit;
49 Hash();
50 Hash(value_type init_value);
51
52 virtual Hash& process(const Object& obj);
53 virtual Hash& process(size_t integer);
54 virtual Hash& process(const std::string& str);
55 virtual Hash& process(const std::u16string& str);
56 virtual Hash& process(const std::vector<uint8_t>& raw);
57 virtual Hash& process(span<const uint8_t> raw);
58
59 template<class T, typename = typename std::enable_if<std::is_enum<T>::value>::type>
60 Hash& process(T v) {
61 return process(static_cast<value_type>(v));
62 }
63
64 template<class It>
65 Hash& process(typename It::iterator v) {
66 return process(std::begin(v), std::end(v));
67 }
68
69
70 template<class T, size_t N>
71 Hash& process(const std::array<T, N>& array) {
72 process(std::begin(array), std::end(array));
73 return *this;
74 }
75
76 template<class T>
77 Hash& process(const std::vector<T>& vector) {
78 process(std::begin(vector), std::end(vector));
79 return *this;
80 }
81
82 template<class T>
83 Hash& process(const std::set<T>& set) {
84 process(std::begin(set), std::end(set));
85 return *this;
86 }
87
88 template<class U, class V>
89 Hash& process(const std::pair<U, V>& p) {
90 process(p.first);
91 process(p.second);
92 return *this;
93 }
94
95 template<class InputIt>
96 Hash& process(InputIt begin, InputIt end) {
97 for (auto&& it = begin; it != end; ++it) {
98 process(*it);
99 }
100 return *this;
101 }
102
103 value_type value() const {
104 return value_;
105 }
106
107 ~Hash() override;
108
109 protected:
110 value_type value_ = 0;
111
112};
113
114LIEF_API Hash::value_type hash(const Object& v);
115LIEF_API Hash::value_type hash(const std::vector<uint8_t>& raw);
116LIEF_API Hash::value_type hash(span<const uint8_t> raw);
117
118template<class Hasher>
119Hash::value_type Hash::hash(const Object& obj) {
120 Hasher hasher;
121 obj.accept(hasher);
122 return hasher.value();
123}
124
125}
126
127
128#endif
Definition hash.hpp:31
Definition Object.hpp:25
Definition Visitor.hpp:219
LIEF namespace.
Definition Abstract/Binary.hpp:32