LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
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);
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;
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);
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
104 return value_;
105 }
106
107 ~Hash() override;
108
109 protected:
110 value_type value_ = 0;
111
112};
113
115LIEF_API Hash::value_type hash(const std::vector<uint8_t>& raw);
117
118template<class Hasher>
120 Hasher hasher;
121 obj.accept(hasher);
122 return hasher.value();
123}
124
125}
126
127
128#endif
Definition hash.hpp:31
static value_type hash(span< const uint8_t > raw)
virtual Hash & process(const std::u16string &str)
virtual Hash & process(span< const uint8_t > raw)
Hash & process(const std::array< T, N > &array)
Definition hash.hpp:71
~Hash() override
static value_type combine(value_type lhs, U rhs)
Definition hash.hpp:43
Hash & process(InputIt begin, InputIt end)
Definition hash.hpp:96
virtual Hash & process(const std::string &str)
virtual Hash & process(size_t integer)
size_t value_type
Definition hash.hpp:33
static value_type hash(const Object &obj)
Definition hash.hpp:119
Hash & process(const std::vector< T > &vector)
Definition hash.hpp:77
Hash(value_type init_value)
virtual Hash & process(const std::vector< uint8_t > &raw)
Hash & process(const std::pair< U, V > &p)
Definition hash.hpp:89
static value_type hash(const void *raw, size_t size)
static value_type hash(const std::vector< uint8_t > &raw)
value_type value() const
Definition hash.hpp:103
Hash & process(T v)
Definition hash.hpp:60
Hash & process(typename It::iterator v)
Definition hash.hpp:65
virtual Hash & process(const Object &obj)
Hash & process(const std::set< T > &set)
Definition hash.hpp:83
Definition Object.hpp:25
virtual void accept(Visitor &visitor) const =0
Definition Visitor.hpp:224
LIEF namespace.
Definition Abstract/Binary.hpp:32
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:41