From 52c017b595af7f65d75b79bb1d4eeeb5acc19580 Mon Sep 17 00:00:00 2001 From: Max Vollmer Date: Mon, 25 Jun 2018 12:05:37 +0200 Subject: [PATCH] Added check to BaseImporter::SearchFileHeaderForToken making sure that a detected token is not in fact just a fraction of a longer token. Microsoft exported binary gltf files were detected as OBJ, because the "gltf " in the string "Microsoft GLTF Exporter 2.4.1.7" was detected as the token "f ". I added a new bool parameter to the method enabling this check. It's default false, and only ObjFileImporter sets it to true, so no other code should be affected. --- code/BaseImporter.cpp | 8 +++++++- code/ObjFileImporter.cpp | 2 +- include/assimp/BaseImporter.h | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/code/BaseImporter.cpp b/code/BaseImporter.cpp index 78979d078..c7d10865e 100644 --- a/code/BaseImporter.cpp +++ b/code/BaseImporter.cpp @@ -143,7 +143,8 @@ void BaseImporter::GetExtensionList(std::set& extensions) { const char** tokens, unsigned int numTokens, unsigned int searchBytes /* = 200 */, - bool tokensSol /* false */) + bool tokensSol /* false */, + bool noAlphaBeforeTokens /* false */) { ai_assert( nullptr != tokens ); ai_assert( 0 != numTokens ); @@ -193,6 +194,11 @@ void BaseImporter::GetExtensionList(std::set& extensions) { if( !r ) { continue; } + // We need to make sure that we didn't accidentially identify the end of another token as our token, + // e.g. in a previous version the "gltf " present in some gltf files was detected as "f " + if (noAlphaBeforeTokens && (r != buffer && isalpha(r[-1]))) { + continue; + } // We got a match, either we don't care where it is, or it happens to // be in the beginning of the file / line if (!tokensSol || r == buffer || r[-1] == '\r' || r[-1] == '\n') { diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index cbf2363a4..64eac6790 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -100,7 +100,7 @@ bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler , } else { // Check file Header static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " }; - return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9 ); + return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9, 200, false, true ); } } diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index 0c0fd110b..7546f9912 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -244,7 +244,8 @@ public: // static utilities const char** tokens, unsigned int numTokens, unsigned int searchBytes = 200, - bool tokensSol = false); + bool tokensSol = false, + bool noAlphaBeforeTokens = false); // ------------------------------------------------------------------- /** @brief Check whether a file has a specific file extension