Skip to content

Commit

Permalink
Fix: Make sure gltf buffer is valid before determining type
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Feb 27, 2024
1 parent fd739d2 commit f8ed5a0
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3820,22 +3820,29 @@ bool fg::AndroidGltfDataBuffer::loadFromAndroidAsset(const fs::path& path, std::

#pragma region Parser
fastgltf::GltfType fg::determineGltfFileType(GltfDataBuffer* buffer) {
// First, check if any of the first four characters is a '{'.
std::array<std::uint8_t, 4> begin = {};
std::memcpy(begin.data(), buffer->bufferPointer, sizeof begin);
for (const auto& i : begin) {
if ((char)i == '{')
return GltfType::glTF;
}
if (buffer->bufferPointer == nullptr)
return GltfType::Invalid;

// We'll try and read a BinaryGltfHeader from the buffer to see if the magic is correct.
BinaryGltfHeader header = {};
std::memcpy(&header, buffer->bufferPointer, sizeof header);
if (header.magic == binaryGltfHeaderMagic) {
return GltfType::GLB;
}
if (buffer->dataSize > sizeof(BinaryGltfHeader)) {
// We'll try and read a BinaryGltfHeader from the buffer to see if the magic is correct.
BinaryGltfHeader header = {};
std::memcpy(&header, buffer->bufferPointer, sizeof header);
if (header.magic == binaryGltfHeaderMagic) {
return GltfType::GLB;
}
}

if (buffer->dataSize > sizeof(std::uint8_t) * 4) {
// First, check if any of the first four characters is a '{'.
std::array<std::uint8_t, 4> begin = {};
std::memcpy(begin.data(), buffer->bufferPointer, sizeof begin);
for (const auto& i : begin) {
if ((char)i == '{')
return GltfType::glTF;
}
}

return GltfType::Invalid;
return GltfType::Invalid;
}

fg::Parser::Parser(Extensions extensionsToLoad) noexcept {
Expand Down

0 comments on commit f8ed5a0

Please sign in to comment.