-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBytecodeHeader.cpp
More file actions
70 lines (58 loc) · 2.3 KB
/
Copy pathBytecodeHeader.cpp
File metadata and controls
70 lines (58 loc) · 2.3 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
// ServerLua: asset header codec, see `BytecodeHeader` in BytecodeHeader.h
#include "Luau/BytecodeHeader.h"
#include <cstring>
namespace Luau
{
void writeBytecodeHeader(std::string& out, const BytecodeHeader& header)
{
ByteWriter writer{out};
writer.writeBytes(kBytecodeHeaderFingerprint.tag, sizeof(kBytecodeHeaderFingerprint.tag));
writer.writeU32(kBytecodeHeaderFingerprint.major);
writer.writeU32(kBytecodeHeaderFingerprint.minor);
size_t section = writer.beginSection();
writer.writeU8((uint8_t)header.isLSL);
writer.writeU32(header.apiVersion);
writer.writeU32((uint32_t)header.stateHandlerMasks.size());
for (uint64_t mask : header.stateHandlerMasks)
writer.writeU64(mask);
writer.writeU32(header.chargedBytecodeSize);
// New fields go here, and bump kBytecodeHeaderFingerprint.minor
writer.endSection(section);
}
bool readBytecodeHeader(const char* data, size_t len, BytecodeHeader& header, size_t& bytecode_start)
{
if (data == nullptr)
return false;
ByteReader reader{data, len};
char tag[sizeof(kBytecodeHeaderFingerprint.tag)];
if (!reader.readBytes(tag, sizeof(tag)) || memcmp(tag, kBytecodeHeaderFingerprint.tag, sizeof(tag)) != 0)
return false;
uint32_t major = 0;
uint32_t minor = 0;
if (!reader.readU32(major) || !reader.readU32(minor) || major != kBytecodeHeaderFingerprint.major)
return false;
ByteReader section{nullptr, 0};
if (!reader.readSection(section))
return false;
uint8_t is_lsl = 0;
uint32_t num_states = 0;
if (!section.readU8(is_lsl) || !section.readU32(header.apiVersion) || !section.readU32(num_states))
return false;
// Eight bytes each, so a count the section can't hold is a corrupt header
// rather than something to allocate for
if (num_states > section.remaining / sizeof(uint64_t))
return false;
header.isLSL = is_lsl != 0;
header.stateHandlerMasks.resize(num_states);
for (uint64_t& mask : header.stateHandlerMasks)
{
if (!section.readU64(mask))
return false;
}
if (!section.readU32(header.chargedBytecodeSize))
return false;
// Fields appended after 6.0 are read here only if the section has them
bytecode_start = len - reader.remaining;
return true;
}
} // namespace Luau