aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/iohash.cpp
blob: a6bf25f6cd2b0f633dd2fae774abd95fbd505c40 (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/iohash.h>

#include <zencore/blake3.h>
#include <zencore/compositebuffer.h>
#include <zencore/string.h>
#include <zencore/testing.h>

#include <gsl/gsl-lite.hpp>

namespace zen {

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)
{
	IoHashStream Hasher;

	for (const SharedBuffer& Segment : Buffer.GetSegments())
	{
		size_t				SegmentSize	  = Segment.GetSize();
		static const size_t BufferingSize = 512 * 1024;
		if (SegmentSize >= (BufferingSize + BufferingSize / 2) && Segment.IsFileReference())
		{
			const IoBuffer SegmentBuffer = Segment.AsIoBuffer();
			size_t		   Offset		 = 0;
			while (Offset < SegmentSize)
			{
				size_t	 ChunkSize = Min<size_t>(SegmentSize - Offset, BufferingSize);
				IoBuffer SubRange(SegmentBuffer, Offset, ChunkSize);
				Hasher.Append(SubRange.GetData(), ChunkSize);
				Offset += ChunkSize;
			}
		}
		else
		{
			Hasher.Append(Segment.GetData(), SegmentSize);
		}
	}

	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;
}

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