Skip to content

Commit

Permalink
Merge branch 'ba2ext' into 'master'
Browse files Browse the repository at this point in the history
Make BA2 extension hash calculation safer (#7784)

Closes #7784

See merge request OpenMW/openmw!3799
  • Loading branch information
jvoisin committed Jan 23, 2024
2 parents 9616b03 + 84ab7af commit 940856b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/bsa/ba2dx10file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace Bsa
return std::nullopt; // folder not found

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 });
if (iter == it->second.end())
return std::nullopt; // file not found
Expand Down
8 changes: 8 additions & 0 deletions components/bsa/ba2file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ namespace Bsa
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
1 change: 1 addition & 0 deletions components/bsa/ba2file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Bsa
{
uint32_t generateHash(const std::string& name);
uint32_t generateExtensionHash(std::string_view extension);
}

#endif
2 changes: 1 addition & 1 deletion components/bsa/ba2gnrlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace Bsa
return FileRecord(); // folder not found, return default which has offset of sInvalidOffset

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 });
if (iter == it->second.end())
return FileRecord(); // file not found, return default which has offset of sInvalidOffset
Expand Down

0 comments on commit 940856b

Please sign in to comment.