LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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 <vector>
20#include <string>
21
22#include "LIEF/visibility.h"
23#include "LIEF/Object.hpp"
24#include "LIEF/Visitor.hpp"
25#include "LIEF/span.hpp"
26#include "LIEF/optional.hpp"
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,
60 typename = typename std::enable_if<std::is_enum<T>::value>::type>
61 Hash& process(T v) {
62 return process(static_cast<value_type>(v));
63 }
64
65 template<class It>
66 Hash& process(typename It::iterator v) {
67 return process(std::begin(v), std::end(v));
68 }
69
70
71 template<class T, size_t N>
72 Hash& process(const std::array<T, N>& array) {
73 process(std::begin(array), std::end(array));
74 return *this;
75 }
76
77 template<class T>
78 Hash& process(const std::vector<T>& vector) {
79 process(std::begin(vector), std::end(vector));
80 return *this;
81 }
82
83 template<class T>
84 Hash& process(const std::set<T>& set) {
85 process(std::begin(set), std::end(set));
86 return *this;
87 }
88
89 template<class T>
90 Hash& process(const optional<T>& opt) {
91 if (opt) {
92 return process(*opt);
93 }
94 return *this;
95 }
96
97 template<class U, class V>
98 Hash& process(const std::pair<U, V>& p) {
99 process(p.first);
100 process(p.second);
101 return *this;
102 }
103
104 template<class InputIt>
105 Hash& process(InputIt begin, InputIt end) {
106 for (auto&& it = begin; it != end; ++it) {
107 process(*it);
108 }
109 return *this;
110 }
111
113 return value_;
114 }
115
116 ~Hash() override;
117
118 protected:
119 value_type value_ = 0;
120};
121
123LIEF_API Hash::value_type hash(const std::vector<uint8_t>& raw);
125
126template<class Hasher>
128 Hasher hasher;
129 obj.accept(hasher);
130 return hasher.value();
131}
132
133}
134
135
136#endif
static value_type hash(span< const uint8_t > raw)
virtual Hash & process(const std::u16string &str)
static value_type hash(const Object &obj)
Definition hash.hpp:127
virtual Hash & process(span< const uint8_t > raw)
Hash & process(const std::array< T, N > &array)
Definition hash.hpp:72
~Hash() override
static value_type combine(value_type lhs, U rhs)
Definition hash.hpp:43
Hash & process(InputIt begin, InputIt end)
Definition hash.hpp:105
virtual Hash & process(const std::string &str)
virtual Hash & process(size_t integer)
size_t value_type
Definition hash.hpp:33
Hash & process(const std::vector< T > &vector)
Definition hash.hpp:78
Hash(value_type init_value)
virtual Hash & process(const std::vector< uint8_t > &raw)
Hash & process(const optional< T > &opt)
Definition hash.hpp:90
Hash & process(const std::pair< U, V > &p)
Definition hash.hpp:98
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:112
Hash & process(T v)
Definition hash.hpp:61
Hash & process(typename It::iterator v)
Definition hash.hpp:66
virtual Hash & process(const Object &obj)
Hash & process(const std::set< T > &set)
Definition hash.hpp:84
Definition Object.hpp:25
virtual void accept(Visitor &visitor) const =0
virtual void visit(const Object &)
Definition optional.hpp:23
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:43