[VFS/STFS] Fix BlockToOffsetSTFS for non-read_only_format (CON) packages

Previous code would give wrong results for those types of packages as the block_shift would cancel itself out, moving it to happen afterward let it give the right result though, probably a mistake while reversing it.
(eg. block 0 would give 0xB000, but that's wrong for non-read_only packages as 0xA000 and 0xB000 are reserved for the first hash-table there, fixed code returns the correct 0xC000 offset)

Also changed it to a multiply & renamed it to blocks_per_hash_table to make it more obvious what the purpose is.
This commit is contained in:
emoose 2021-04-17 13:59:28 +01:00 committed by Rick Gibbed
parent 14783b03f2
commit 509f33ba27
2 changed files with 7 additions and 10 deletions

View File

@ -561,13 +561,10 @@ StfsContainerDevice::Error StfsContainerDevice::ReadSTFS() {
return Error::kSuccess; return Error::kSuccess;
} }
size_t StfsContainerDevice::BlockToOffsetSTFS(uint64_t block_index) { size_t StfsContainerDevice::BlockToOffsetSTFS(uint64_t block_index) const {
uint64_t block; uint32_t blocks_per_hash_table = 1;
uint32_t block_shift = 0; if (!header_.metadata.stfs_volume_descriptor.flags.read_only_format) {
if (((header_.header.header_size + 0x0FFF) & 0xB000) == 0xB000 || blocks_per_hash_table = 2;
!header_.metadata.stfs_volume_descriptor.flags.read_only_format) {
block_shift =
header_.header.magic == XContentPackageType::kPackageTypeCon ? 1 : 0;
} }
// For every level there is a hash table // For every level there is a hash table
@ -576,9 +573,9 @@ size_t StfsContainerDevice::BlockToOffsetSTFS(uint64_t block_index) {
// Level 2: hash table of next 170 level 1 hash tables // Level 2: hash table of next 170 level 1 hash tables
// And so on... // And so on...
uint64_t base = kBlocksPerHashLevel[0]; uint64_t base = kBlocksPerHashLevel[0];
block = block_index; uint64_t block = block_index;
for (uint32_t i = 0; i < 3; i++) { for (uint32_t i = 0; i < 3; i++) {
block += (block_index + (base << block_shift)) / (base << block_shift); block += ((block_index + base) / base) * blocks_per_hash_table;
if (block_index < base) { if (block_index < base) {
break; break;
} }

View File

@ -462,7 +462,7 @@ class StfsContainerDevice : public Device {
void BlockToOffsetSVOD(size_t sector, size_t* address, size_t* file_index); void BlockToOffsetSVOD(size_t sector, size_t* address, size_t* file_index);
Error ReadSTFS(); Error ReadSTFS();
size_t BlockToOffsetSTFS(uint64_t block); size_t BlockToOffsetSTFS(uint64_t block_index) const;
StfsHashEntry GetBlockHash(const uint8_t* map_ptr, uint32_t block_index, StfsHashEntry GetBlockHash(const uint8_t* map_ptr, uint32_t block_index,
uint32_t table_offset); uint32_t table_offset);