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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zenutil/chunkblock.h>
#include <zencore/compactbinarybuilder.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <EASTL/vector.h>
namespace zen {
using namespace std::literals;
ChunkBlockDescription
ParseChunkBlockDescription(const CbObjectView& BlockObject)
{
ChunkBlockDescription Result;
Result.BlockHash = BlockObject["rawHash"sv].AsHash();
if (Result.BlockHash != IoHash::Zero)
{
Result.HeaderSize = BlockObject["headerSize"sv].AsUInt64();
CbArrayView ChunksArray = BlockObject["rawHashes"sv].AsArrayView();
Result.ChunkRawHashes.reserve(ChunksArray.Num());
for (CbFieldView ChunkView : ChunksArray)
{
Result.ChunkRawHashes.push_back(ChunkView.AsHash());
}
CbArrayView ChunkRawLengthsArray = BlockObject["chunkRawLengths"sv].AsArrayView();
Result.ChunkRawLengths.reserve(ChunkRawLengthsArray.Num());
for (CbFieldView ChunkView : ChunkRawLengthsArray)
{
Result.ChunkRawLengths.push_back(ChunkView.AsUInt32());
}
CbArrayView ChunkCompressedLengthsArray = BlockObject["chunkCompressedLengths"sv].AsArrayView();
Result.ChunkCompressedLengths.reserve(ChunkCompressedLengthsArray.Num());
for (CbFieldView ChunkView : ChunkCompressedLengthsArray)
{
Result.ChunkCompressedLengths.push_back(ChunkView.AsUInt32());
}
}
return Result;
}
eastl::vector<ChunkBlockDescription>
ParseChunkBlockDescriptionList(const CbObjectView& BlocksObject)
{
if (!BlocksObject)
{
return {};
}
eastl::vector<ChunkBlockDescription> Result;
CbArrayView Blocks = BlocksObject["blocks"].AsArrayView();
Result.reserve(Blocks.Num());
for (CbFieldView BlockView : Blocks)
{
CbObjectView BlockObject = BlockView.AsObjectView();
Result.emplace_back(ParseChunkBlockDescription(BlockObject));
}
return Result;
}
CbObject
BuildChunkBlockDescription(const ChunkBlockDescription& Block, CbObjectView MetaData)
{
ZEN_ASSERT(Block.BlockHash != IoHash::Zero);
ZEN_ASSERT(Block.HeaderSize > 0);
ZEN_ASSERT(Block.ChunkRawLengths.size() == Block.ChunkRawHashes.size());
ZEN_ASSERT(Block.ChunkCompressedLengths.size() == Block.ChunkRawHashes.size());
CbObjectWriter Writer;
Writer.AddHash("rawHash"sv, Block.BlockHash);
Writer.AddInteger("headerSize"sv, Block.HeaderSize);
Writer.BeginArray("rawHashes"sv);
{
for (const IoHash& ChunkHash : Block.ChunkRawHashes)
{
Writer.AddHash(ChunkHash);
}
}
Writer.EndArray();
Writer.BeginArray("chunkRawLengths");
{
for (uint32_t ChunkSize : Block.ChunkRawLengths)
{
Writer.AddInteger(ChunkSize);
}
}
Writer.EndArray();
Writer.BeginArray("chunkCompressedLengths");
{
for (uint32_t ChunkSize : Block.ChunkCompressedLengths)
{
Writer.AddInteger(ChunkSize);
}
}
Writer.EndArray();
Writer.AddObject("metadata", MetaData);
return Writer.Save();
}
ChunkBlockDescription
GetChunkBlockDescription(const SharedBuffer& BlockPayload, const IoHash& RawHash)
{
ChunkBlockDescription BlockDescription = {{.BlockHash = IoHash::HashBuffer(BlockPayload)}};
if (BlockDescription.BlockHash != RawHash)
{
throw std::runtime_error(fmt::format("Block {} content hash {} does not match block hash", RawHash, BlockDescription.BlockHash));
}
if (IterateChunkBlock(
BlockPayload,
[&BlockDescription, RawHash](CompressedBuffer&& Chunk, const IoHash& AttachmentHash) {
if (CompositeBuffer Decompressed = Chunk.DecompressToComposite(); Decompressed)
{
IoHash ChunkHash = IoHash::HashBuffer(Decompressed.Flatten());
if (ChunkHash != AttachmentHash)
{
throw std::runtime_error(
fmt::format("Chunk {} in block {} content hash {} does not match chunk", AttachmentHash, RawHash, ChunkHash));
}
BlockDescription.ChunkRawHashes.push_back(AttachmentHash);
BlockDescription.ChunkRawLengths.push_back(gsl::narrow<uint32_t>(Decompressed.GetSize()));
BlockDescription.ChunkCompressedLengths.push_back(gsl::narrow<uint32_t>(Chunk.GetCompressedSize()));
}
else
{
throw std::runtime_error(fmt::format("Chunk {} in block {} is not a compressed buffer", AttachmentHash, RawHash));
}
},
BlockDescription.HeaderSize))
{
return BlockDescription;
}
else
{
throw std::runtime_error(fmt::format("Block {} is malformed", RawHash));
}
}
CompressedBuffer
GenerateChunkBlock(eastl::vector<std::pair<IoHash, FetchChunkFunc>>&& FetchChunks, ChunkBlockDescription& OutBlock)
{
const size_t ChunkCount = FetchChunks.size();
eastl::vector<SharedBuffer> ChunkSegments;
ChunkSegments.resize(1);
ChunkSegments.reserve(1 + ChunkCount);
OutBlock.ChunkRawHashes.reserve(ChunkCount);
OutBlock.ChunkRawLengths.reserve(ChunkCount);
OutBlock.ChunkCompressedLengths.reserve(ChunkCount);
{
IoBuffer TempBuffer(ChunkCount * 9);
MutableMemoryView View = TempBuffer.GetMutableView();
uint8_t* BufferStartPtr = reinterpret_cast<uint8_t*>(View.GetData());
uint8_t* BufferEndPtr = BufferStartPtr;
BufferEndPtr += WriteVarUInt(gsl::narrow<uint64_t>(ChunkCount), BufferEndPtr);
for (const auto& It : FetchChunks)
{
std::pair<uint64_t, CompressedBuffer> Chunk = It.second(It.first);
uint64_t ChunkSize = 0;
eastl::span<const SharedBuffer> Segments = Chunk.second.GetCompressed().GetSegments();
for (const SharedBuffer& Segment : Segments)
{
ZEN_ASSERT(Segment.IsOwned());
ChunkSize += Segment.GetSize();
ChunkSegments.push_back(Segment);
}
BufferEndPtr += WriteVarUInt(ChunkSize, BufferEndPtr);
OutBlock.ChunkRawHashes.push_back(It.first);
OutBlock.ChunkRawLengths.push_back(gsl::narrow<uint32_t>(Chunk.first));
OutBlock.ChunkCompressedLengths.push_back(gsl::narrow<uint32_t>(ChunkSize));
}
ZEN_ASSERT(BufferEndPtr <= View.GetDataEnd());
ptrdiff_t TempBufferLength = std::distance(BufferStartPtr, BufferEndPtr);
ChunkSegments[0] = SharedBuffer(IoBuffer(TempBuffer, 0, gsl::narrow<size_t>(TempBufferLength)));
OutBlock.HeaderSize = TempBufferLength;
}
CompressedBuffer CompressedBlock =
CompressedBuffer::Compress(CompositeBuffer(std::move(ChunkSegments)), OodleCompressor::Mermaid, OodleCompressionLevel::None);
OutBlock.BlockHash = CompressedBlock.DecodeRawHash();
return CompressedBlock;
}
bool
IterateChunkBlock(const SharedBuffer& BlockPayload,
std::function<void(CompressedBuffer&& Chunk, const IoHash& AttachmentHash)> Visitor,
uint64_t& OutHeaderSize)
{
ZEN_ASSERT(BlockPayload);
if (BlockPayload.GetSize() < 1)
{
return false;
}
MemoryView BlockView = BlockPayload.GetView();
const uint8_t* ReadPtr = reinterpret_cast<const uint8_t*>(BlockView.GetData());
uint32_t NumberSize;
uint64_t ChunkCount = ReadVarUInt(ReadPtr, NumberSize);
ReadPtr += NumberSize;
eastl::vector<uint64_t> ChunkSizes;
ChunkSizes.reserve(ChunkCount);
while (ChunkCount--)
{
ChunkSizes.push_back(ReadVarUInt(ReadPtr, NumberSize));
ReadPtr += NumberSize;
}
uint64_t Offset = std::distance((const uint8_t*)BlockView.GetData(), ReadPtr);
OutHeaderSize = Offset;
for (uint64_t ChunkSize : ChunkSizes)
{
IoBuffer Chunk(BlockPayload.AsIoBuffer(), Offset, ChunkSize);
IoHash AttachmentRawHash;
uint64_t AttachmentRawSize;
CompressedBuffer CompressedChunk = CompressedBuffer::FromCompressed(SharedBuffer(Chunk), AttachmentRawHash, AttachmentRawSize);
ZEN_ASSERT_SLOW(IoHash::HashBuffer(CompressedChunk.DecompressToComposite()) == AttachmentRawHash);
if (!CompressedChunk)
{
ZEN_ERROR("Invalid chunk in block");
return false;
}
Visitor(std::move(CompressedChunk), AttachmentRawHash);
Offset += ChunkSize;
ZEN_ASSERT(Offset <= BlockView.GetSize());
}
return true;
};
} // namespace zen
|