13 if (!file.open(QIODevice::ReadOnly)) {
14 std::cout <<
"ERROR: Cannot open the file " << filepath.toStdString() <<
std::endl;
15 std::cout << file.errorString().toStdString() <<
std::endl;
19 auto bytes = file.readAll();
20 if (!parsePreambule(bytes)) {
25bool WasmBinary::parsePreambule(QByteArrayView data)
27 const auto preambuleSize = 24;
28 if (data.size() < preambuleSize) {
29 std::cout <<
"ERROR: Preambule of binary shorter than expected!" <<
std::endl;
32 uint32_t int32View[6];
33 std::memcpy(int32View, data.data(),
sizeof(int32View));
34 if (int32View[0] != 0x6d736100) {
35 std::cout <<
"ERROR: Magic WASM number not found in binary. Binary corrupted?" <<
std::endl;
44 const auto sectionStart = 9;
45 size_t offset = sectionStart;
46 auto sectionSize = getLeb(data, offset);
47 auto sectionEnd = sectionStart + sectionSize;
48 auto name = getString(data, offset);
49 if (name !=
"dylink.0") {
51 std::cout <<
"ERROR: dylink.0 was not found in supposedly dynamically linked module"
56 const auto WASM_DYLINK_NEEDED = 0x2;
57 while (offset < sectionEnd) {
58 auto subsectionType = data[offset++];
59 auto subsectionSize = getLeb(data, offset);
60 if (subsectionType == WASM_DYLINK_NEEDED) {
61 auto neededDynlibsCount = getLeb(data, offset);
62 while (neededDynlibsCount--) {
63 dependencies.append(getString(data, offset));
66 offset += subsectionSize;
72size_t
WasmBinary::getLeb(QByteArrayView data, size_t &offset)
77 auto byte = data[offset++];
78 ret += (byte & 0x7f) * mul;
86QString
WasmBinary::getString(QByteArrayView data, size_t &offset)
88 auto length = getLeb(data, offset);
90 return QString::fromUtf8(data.sliced(offset - length, length));
WasmBinary(QString filepath)