-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathmultihash.cpp
More file actions
170 lines (142 loc) · 4.43 KB
/
multihash.cpp
File metadata and controls
170 lines (142 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <libp2p/multi/multihash.hpp>
#include <boost/container_hash/hash.hpp>
#include <libp2p/basic/varint_prefix_reader.hpp>
#include <libp2p/common/types.hpp>
#include <libp2p/log/logger.hpp>
#include <qtils/hex.hpp>
#include <qtils/unhex.hpp>
OUTCOME_CPP_DEFINE_CATEGORY(libp2p::multi, Multihash::Error, e) {
using E = libp2p::multi::Multihash::Error;
switch (e) {
case E::ZERO_INPUT_LENGTH:
return "The length encoded in the header is zero";
case E::INCONSISTENT_LENGTH:
return "The length encoded in the input data header doesn't match the "
"actual length of the input data";
case E::INPUT_TOO_LONG:
return "The length of the input exceeds the maximum length of "
+ std::to_string(libp2p::multi::Multihash::kMaxHashLength);
case E::INPUT_TOO_SHORT:
return "The length of the input is less than the required minimum of two "
"bytes for the multihash header";
default:
return "Unknown error";
}
}
namespace libp2p::multi {
Multihash::Multihash(HashType type, BytesIn hash)
: data_(std::make_shared<Data>(type, hash)) {}
namespace {
template <typename Buffer>
inline void appendVarint(Buffer &buffer, uint64_t t) {
do {
uint8_t byte = t & 0x7F;
t >>= 7;
if (t != 0) {
byte |= 0x80;
}
buffer.push_back(byte);
} while (t > 0);
}
} // namespace
Multihash::Data::Data(HashType t, BytesIn h) : type(t) {
bytes.reserve(h.size() + 4);
appendVarint(bytes, type);
BOOST_ASSERT(h.size() <= std::numeric_limits<uint8_t>::max());
bytes.push_back(static_cast<uint8_t>(h.size()));
hash_offset = bytes.size();
bytes.insert(bytes.end(), h.begin(), h.end());
std_hash = boost::hash_range(bytes.begin(), bytes.end());
}
const Multihash::Data &Multihash::data() const {
#if NDEBUG
if (data_ == nullptr) {
log::createLogger("Multihash")->critical("attempt to use moved object");
throw std::runtime_error("attempt to use moved multihash");
}
#else
BOOST_ASSERT(data_);
#endif
return *data_;
}
size_t Multihash::stdHash() const {
return data().std_hash;
}
outcome::result<Multihash> Multihash::create(HashType type, BytesIn hash) {
if (hash.size() > kMaxHashLength) {
return Error::INPUT_TOO_LONG;
}
return Multihash{type, hash};
}
outcome::result<Multihash> Multihash::createFromHex(std::string_view hex) {
OUTCOME_TRY(buf, qtils::unhex(hex));
return Multihash::createFromBytes(buf);
}
outcome::result<Multihash> Multihash::createFromBytes(BytesIn b) {
if (b.size() < kHeaderSize) {
return Error::INPUT_TOO_SHORT;
}
basic::VarintPrefixReader vr;
if (vr.consume(b) != basic::VarintPrefixReader::kReady) {
return Error::INPUT_TOO_SHORT;
}
const auto type = static_cast<HashType>(vr.value());
if (b.empty()) {
return Error::INPUT_TOO_SHORT;
}
const uint8_t length = b[0];
BytesIn hash = b.subspan(1);
if (length == 0) {
return Error::ZERO_INPUT_LENGTH;
}
if (hash.size() != length) {
return Error::INCONSISTENT_LENGTH;
}
return Multihash::create(type, hash);
}
const HashType &Multihash::getType() const {
return data().type;
}
BytesIn Multihash::getHash() const {
const auto &d = data();
return BytesIn(d.bytes).subspan(d.hash_offset);
}
std::string Multihash::toHex() const {
return fmt::format("{:X}", data().bytes);
}
const Bytes &Multihash::toBuffer() const & {
return data().bytes;
}
Bytes Multihash::toBuffer() && {
if (data_ && data_.use_count() == 1) {
auto data = std::move(data_);
data_ = nullptr;
return std::move(data->bytes);
}
return data().bytes;
}
bool Multihash::operator==(const Multihash &other) const {
const auto &a = data();
const auto &b = other.data();
if (data_ == other.data_) {
return true;
}
return a.bytes == b.bytes && a.type == b.type;
}
bool Multihash::operator!=(const Multihash &other) const {
return !(this->operator==(other));
}
bool Multihash::operator<(const class libp2p::multi::Multihash &other) const {
const auto &a = data();
const auto &b = other.data();
if (a.type == b.type) {
return a.bytes < b.bytes;
}
return a.type < b.type;
}
} // namespace libp2p::multi