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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/iohash.h>
#include <zencore/blake3.h>
#include <zencore/compositebuffer.h>
#include <zencore/filesystem.h>
#include <zencore/string.h>
#include <zencore/testing.h>
#include <gsl/gsl-lite.hpp>
namespace zen {
static const uint8_t MaxData[20] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
const IoHash IoHash::Max = IoHash::MakeFrom(MaxData); // Initialized to all 0xff
const IoHash IoHash::Zero{}; // Initialized to all zeros
IoHash
IoHash::HashBuffer(const void* data, size_t byteCount)
{
BLAKE3 b3 = BLAKE3::HashMemory(data, byteCount);
IoHash io;
memcpy(io.Hash, b3.Hash, sizeof io.Hash);
return io;
}
IoHash
IoHash::HashBuffer(const CompositeBuffer& Buffer, std::atomic<uint64_t>* ProcessedBytes)
{
IoHashStream Hasher;
for (const SharedBuffer& Segment : Buffer.GetSegments())
{
size_t SegmentSize = Segment.GetSize();
static const uint64_t BufferingSize = 256u * 1024u;
IoBufferFileReference FileRef;
if (SegmentSize >= (BufferingSize + BufferingSize / 2) && Segment.GetFileReference(FileRef))
{
ScanFile(FileRef.FileHandle,
FileRef.FileChunkOffset,
FileRef.FileChunkSize,
BufferingSize,
[&Hasher, ProcessedBytes](const void* Data, size_t Size) {
Hasher.Append(Data, Size);
if (ProcessedBytes != nullptr)
{
ProcessedBytes->fetch_add(Size);
}
});
}
else
{
Hasher.Append(Segment.GetData(), SegmentSize);
if (ProcessedBytes != nullptr)
{
ProcessedBytes->fetch_add(SegmentSize);
}
}
}
return Hasher.GetHash();
}
IoHash
IoHash::HashBuffer(const IoBuffer& Buffer, std::atomic<uint64_t>* ProcessedBytes)
{
IoHashStream Hasher;
size_t BufferSize = Buffer.GetSize();
static const uint64_t BufferingSize = 256u * 1024u;
IoBufferFileReference FileRef;
if (BufferSize >= (BufferingSize + BufferingSize / 2) && Buffer.GetFileReference(FileRef))
{
ScanFile(FileRef.FileHandle,
FileRef.FileChunkOffset,
FileRef.FileChunkSize,
BufferingSize,
[&Hasher, ProcessedBytes](const void* Data, size_t Size) {
Hasher.Append(Data, Size);
if (ProcessedBytes != nullptr)
{
ProcessedBytes->fetch_add(Size);
}
});
}
else
{
Hasher.Append(Buffer.GetData(), BufferSize);
if (ProcessedBytes != nullptr)
{
ProcessedBytes->fetch_add(BufferSize);
}
}
return Hasher.GetHash();
}
IoHash
IoHash::FromHexString(const char* string)
{
return FromHexString({string, sizeof(IoHash::Hash) * 2});
}
IoHash
IoHash::FromHexString(std::string_view string)
{
ZEN_ASSERT(string.size() == 2 * sizeof(IoHash::Hash));
IoHash io;
ParseHexBytes(string.data(), string.size(), io.Hash);
return io;
}
bool
IoHash::TryParse(std::string_view Str, IoHash& Hash)
{
using namespace std::literals;
if (Str.size() == IoHash::StringLength)
{
return ParseHexBytes(Str.data(), Str.size(), Hash.Hash);
}
if (Str.starts_with("0x"sv))
{
return TryParse(Str.substr(2), Hash);
}
return false;
}
const char*
IoHash::ToHexString(char* outString /* 40 characters + NUL terminator */) const
{
ToHexBytes(Hash, sizeof(IoHash), outString);
outString[2 * sizeof(IoHash)] = '\0';
return outString;
}
StringBuilderBase&
IoHash::ToHexString(StringBuilderBase& outBuilder) const
{
String_t Str;
ToHexString(Str);
outBuilder.AppendRange(Str, &Str[StringLength]);
return outBuilder;
}
std::string
IoHash::ToHexString() const
{
String_t Str;
ToHexString(Str);
return Str;
}
} // namespace zen
|