Fix handling <4 byte BIN data.

Fix handling GLB file with empty CHUNK1(BIN).
This commit is contained in:
Syoyo Fujita 2022-09-18 21:01:39 +09:00
parent a778c089d1
commit 612e57816f
2 changed files with 15 additions and 8 deletions

2
.gitignore vendored
View File

@ -71,7 +71,7 @@ tests/issue-97.gltf
tests/issue-261.gltf tests/issue-261.gltf
# unignore # unignore
!/Makefile !Makefile
!examples/build-gltf/Makefile !examples/build-gltf/Makefile
!examples/raytrace/cornellbox_suzanne.obj !examples/raytrace/cornellbox_suzanne.obj
!tests/Makefile !tests/Makefile

View File

@ -6311,19 +6311,22 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
} }
} }
//std::cout << "header_and_json_size = " << header_and_json_size << "\n";
//std::cout << "length = " << length << "\n";
// Chunk1(BIN) data // Chunk1(BIN) data
// The spec says: When the binary buffer is empty or when it is stored by other means, this chunk SHOULD be omitted. // The spec says: When the binary buffer is empty or when it is stored by other means, this chunk SHOULD be omitted.
if (header_and_json_size <= uint64_t(length)) { // So when header + JSON data == binary size, Chunk1 is omitted.
if (header_and_json_size == uint64_t(length)) {
// Just in case...
bin_data_ = nullptr; bin_data_ = nullptr;
bin_size_ = 0; bin_size_ = 0;
} else { } else {
// Read Chunk1 info(BIN data) // Read Chunk1 info(BIN data)
// At least Chunk1 should have 12 bytes(8 bytes(header) + 4 bytes(bin content)) // At least Chunk1 should have 12 bytes(8 bytes(header) + 4 bytes(bin payload could be 1~3 bytes, but need to be aliged to 4 bytes)
if ((header_and_json_size + 12ull) >= uint64_t(length)) { if ((header_and_json_size + 12ull) > uint64_t(length)) {
if (err) { if (err) {
(*err) = "Insufficient storage space for Chunk1(BIN data). At least Chunk1 Must have 12bytes or more bytes, but got " + std::to_string((header_and_json_size + 12ull) - uint64_t(length)) + ".\n"; (*err) = "Insufficient storage space for Chunk1(BIN data). At least Chunk1 Must have 4 bytes or more bytes, but got " + std::to_string((header_and_json_size + 12ull) - uint64_t(length)) + ".\n";
} }
return false; return false;
} }
@ -6335,6 +6338,8 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
memcpy(&chunk1_format, bytes + header_and_json_size + 4, 4); memcpy(&chunk1_format, bytes + header_and_json_size + 4, 4);
swap4(&chunk1_format); swap4(&chunk1_format);
//std::cout << "chunk1_length = " << chunk1_length << "\n";
if (chunk1_length < 4) { if (chunk1_length < 4) {
if (err) { if (err) {
(*err) = "Insufficient Chunk1(BIN) data size."; (*err) = "Insufficient Chunk1(BIN) data size.";
@ -6363,6 +6368,8 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
return false; return false;
} }
//std::cout << "chunk1_length = " << chunk1_length << "\n";
bin_data_ = bytes + header_and_json_size + bin_data_ = bytes + header_and_json_size +
8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format) 8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format)