LIEF: Library to Instrument Executable Formats Version 2.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 <string_view>
20#include <cstdint>
21#include <string>
22#include <vector>
23
24#include "LIEF/Object.hpp"
25#include "LIEF/Visitor.hpp"
26#include "LIEF/span.hpp"
27#include "LIEF/visibility.h"
28#include <optional>
29
30namespace LIEF {
31
32
33class LIEF_API Hash : public Visitor {
34 public:
35 using value_type = size_t;
36 template<class H = Hash>
37 static value_type hash(const Object& obj);
38
39 static value_type hash(const std::vector<uint8_t>& raw);
41 static value_type hash(const void* raw, size_t size);
42
43 // combine two elements to produce a size_t.
44 template<typename U = value_type>
45 static value_type combine(value_type lhs, U rhs) {
46 return (lhs ^ rhs) + 0x9e3779b9 + (lhs << 6) + (rhs >> 2);
47 }
48
49 public:
50 using Visitor::visit;
52 Hash(value_type init_value);
53
54 virtual Hash& process(const Object& obj);
55 virtual Hash& process(size_t integer);
56 virtual Hash& process(const std::string& str);
57 virtual Hash& process(const std::u16string& str);
58 virtual Hash& process(const std::vector<uint8_t>& raw);
60
61 Hash& process(std::string_view str) {
62 return process(std::string(str));
63 }
64
65 template<size_t N>
66 Hash& process(const char (&str)[N]) {
67 size_t size = 0;
68 while (size < N && str[size] != '\0') {
69 ++size;
70 }
71 return process(std::string_view(str, size));
72 }
73
74 template<class T, typename = std::enable_if_t<std::is_enum_v<T>>>
75 Hash& process(T v) {
76 return process(static_cast<value_type>(v));
77 }
78
79 template<class It>
80 Hash& process(typename It::iterator v) {
81 return process(std::begin(v), std::end(v));
82 }
83
84
85 template<class T, size_t N>
86 Hash& process(const std::array<T, N>& array) {
87 process(std::begin(array), std::end(array));
88 return *this;
89 }
90
91 template<class T>
92 Hash& process(const std::vector<T>& vector) {
93 process(std::begin(vector), std::end(vector));
94 return *this;
95 }
96
97 template<class T>
98 Hash& process(const std::set<T>& set) {
99 process(std::begin(set), std::end(set));
100 return *this;
101 }
102
103 template<class T>
104 Hash& process(const std::optional<T>& opt) {
105 if (opt) {
106 return process(*opt);
107 }
108 return *this;
109 }
110
111 template<class U, class V>
112 Hash& process(const std::pair<U, V>& p) {
113 process(p.first);
114 process(p.second);
115 return *this;
116 }
117
118 template<class InputIt>
119 Hash& process(InputIt begin, InputIt end) {
120 for (auto&& it = begin; it != end; ++it) {
121 process(*it);
122 }
123 return *this;
124 }
125
127 return value_;
128 }
129
130 ~Hash() override;
131
132 protected:
133 value_type value_ = 0;
134};
135
137LIEF_API Hash::value_type hash(const std::vector<uint8_t>& raw);
139
140template<class Hasher>
142 Hasher hasher;
143 obj.accept(hasher);
144 return hasher.value();
145}
146
147}
148
149
150#endif
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:86
Hash & process(const char(&str)[N])
Definition hash.hpp:66
~Hash() override
static value_type combine(value_type lhs, U rhs)
Definition hash.hpp:45
Hash & process(InputIt begin, InputIt end)
Definition hash.hpp:119
virtual Hash & process(const std::string &str)
virtual Hash & process(size_t integer)
size_t value_type
Definition hash.hpp:35
Hash & process(const std::optional< T > &opt)
Definition hash.hpp:104
static value_type hash(const Object &obj)
Definition hash.hpp:141
Hash & process(const std::vector< T > &vector)
Definition hash.hpp:92
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:112
Hash & process(std::string_view str)
Definition hash.hpp:61
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:126
Hash & process(T v)
Definition hash.hpp:75
Hash & process(typename It::iterator v)
Definition hash.hpp:80
virtual Hash & process(const Object &obj)
Hash & process(const std::set< T > &set)
Definition hash.hpp:98
Definition Object.hpp:25
virtual void accept(Visitor &visitor) const =0
virtual void visit(const Object &)
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:45