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(std::string_view str);
57 virtual Hash& process(const std::u16string& str);
58 virtual Hash& process(const std::vector<uint8_t>& raw);
60
61 template<size_t N>
62 Hash& process(const char (&str)[N]) {
63 size_t size = 0;
64 while (size < N && str[size] != '\0') {
65 ++size;
66 }
67 return process(std::string_view(str, size));
68 }
69
70 template<class T, typename = std::enable_if_t<std::is_enum_v<T>>>
71 Hash& process(T v) {
72 return process(static_cast<value_type>(v));
73 }
74
75 template<class It>
76 Hash& process(typename It::iterator v) {
77 return process(std::begin(v), std::end(v));
78 }
79
80
81 template<class T, size_t N>
82 Hash& process(const std::array<T, N>& array) {
83 process(std::begin(array), std::end(array));
84 return *this;
85 }
86
87 template<class T>
88 Hash& process(const std::vector<T>& vector) {
89 process(std::begin(vector), std::end(vector));
90 return *this;
91 }
92
93 template<class T>
94 Hash& process(const std::set<T>& set) {
95 process(std::begin(set), std::end(set));
96 return *this;
97 }
98
99 template<class T>
100 Hash& process(const std::optional<T>& opt) {
101 if (opt) {
102 return process(*opt);
103 }
104 return *this;
105 }
106
107 template<class U, class V>
108 Hash& process(const std::pair<U, V>& p) {
109 process(p.first);
110 process(p.second);
111 return *this;
112 }
113
114 template<class InputIt>
115 // NOLINTNEXTLINE(performance-unnecessary-value-param)
116 Hash& process(InputIt begin, InputIt end) {
117 for (auto&& it = begin; it != end; ++it) {
118 process(*it);
119 }
120 return *this;
121 }
122
124 return value_;
125 }
126
127 ~Hash() override;
128
129 protected:
130 value_type value_ = 0;
131};
132
134LIEF_API Hash::value_type hash(const std::vector<uint8_t>& raw);
136
137template<class Hasher>
139 Hasher hasher;
140 obj.accept(hasher);
141 return hasher.value();
142}
143
144}
145
146
147#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:82
Hash & process(const char(&str)[N])
Definition hash.hpp:62
virtual Hash & process(std::string_view str)
~Hash() override
static value_type combine(value_type lhs, U rhs)
Definition hash.hpp:45
Hash & process(InputIt begin, InputIt end)
Definition hash.hpp:116
virtual Hash & process(size_t integer)
size_t value_type
Definition hash.hpp:35
Hash & process(const std::optional< T > &opt)
Definition hash.hpp:100
static value_type hash(const Object &obj)
Definition hash.hpp:138
Hash & process(const std::vector< T > &vector)
Definition hash.hpp:88
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:108
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:123
Hash & process(T v)
Definition hash.hpp:71
Hash & process(typename It::iterator v)
Definition hash.hpp:76
virtual Hash & process(const Object &obj)
Hash & process(const std::set< T > &set)
Definition hash.hpp:94
Definition Object.hpp:25
virtual void accept(Visitor &visitor) const =0
virtual void visit(const Object &)
LIEF namespace.
Definition Abstract/Binary.hpp:42
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:45