Make BA2 extension hash calculation safer (#7784)

ini_importer_tests
Alexei Kotov 11 months ago
parent c45c2355ae
commit 84ab7afd44

@ -177,7 +177,7 @@ namespace Bsa
return std::nullopt; // folder not found return std::nullopt; // folder not found
uint32_t fileHash = generateHash(fileName); uint32_t fileHash = generateHash(fileName);
uint32_t extHash = *reinterpret_cast<const uint32_t*>(ext.data() + 1); uint32_t extHash = generateExtensionHash(ext);
auto iter = it->second.find({ fileHash, extHash }); auto iter = it->second.find({ fileHash, extHash });
if (iter == it->second.end()) if (iter == it->second.end())
return std::nullopt; // file not found return std::nullopt; // file not found

@ -46,4 +46,12 @@ namespace Bsa
return result; return result;
} }
uint32_t generateExtensionHash(std::string_view extension)
{
uint32_t result = 0;
for (size_t i = 0; i < 4 && i < extension.size() - 1; i++)
result |= static_cast<uint8_t>(extension[i + 1]) << (8 * i);
return result;
}
} // namespace Bsa } // namespace Bsa

@ -7,6 +7,7 @@
namespace Bsa namespace Bsa
{ {
uint32_t generateHash(const std::string& name); uint32_t generateHash(const std::string& name);
uint32_t generateExtensionHash(std::string_view extension);
} }
#endif #endif

@ -172,7 +172,7 @@ namespace Bsa
return FileRecord(); // folder not found, return default which has offset of sInvalidOffset return FileRecord(); // folder not found, return default which has offset of sInvalidOffset
uint32_t fileHash = generateHash(fileName); uint32_t fileHash = generateHash(fileName);
uint32_t extHash = *reinterpret_cast<const uint32_t*>(ext.data() + 1); uint32_t extHash = generateExtensionHash(ext);
auto iter = it->second.find({ fileHash, extHash }); auto iter = it->second.find({ fileHash, extHash });
if (iter == it->second.end()) if (iter == it->second.end())
return FileRecord(); // file not found, return default which has offset of sInvalidOffset return FileRecord(); // file not found, return default which has offset of sInvalidOffset

Loading…
Cancel
Save