Skip to content

Commit

Permalink
xex: don't include zeroes when converting VA -> file offset, warn if …
Browse files Browse the repository at this point in the history
…VA is part of the zero-block
  • Loading branch information
emoose committed Feb 7, 2021
1 parent 10ec0dd commit d9de36c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion xex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ uint32_t XEXFile::xex_va_to_offset(void* file, uint32_t va)

xex_opt::XexDataFormat comp_format = (xex_opt::XexDataFormat)(uint16_t)data_descriptor_->Format;

// convert VA to RVA if necessary
if (va >= base_address())
va -= base_address();

Expand All @@ -925,7 +926,7 @@ uint32_t XEXFile::xex_va_to_offset(void* file, uint32_t va)
for (int i = 0; i < num_blocks; i++)
{
const auto& block = xex_blocks[i];
int block_end = position + block.DataSize + block.ZeroSize;
int block_end = position + block.DataSize;
if (va >= position && va < block_end)
return xex_header_.SizeOfHeaders + real_position + (va - position);

Expand Down
16 changes: 14 additions & 2 deletions xex1tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,17 +871,29 @@ int main(int argc, char* argv[])
if (xex.header().Magic != MAGIC_XEX2)
printf("XEX isn't XEX2, addresses might not be correct!\n");

uint32_t result = 0;
if (rva >= xex.base_address())
{
result = xex.xex_va_to_offset(file, rva);
printf("Virtual Address -> File Offset\n");
printf("Virtual Addr: 0x%X\n", rva);
printf("File Offset: 0x%X\n", xex.xex_va_to_offset(file, rva));
printf("File Offset: 0x%X\n", result);
}
else
{
result = xex.xex_offset_to_va(file, rva);
printf("File Offset -> Virtual Address\n");
printf("File Offset: 0x%X\n", rva);
printf("Virtual Addr: 0x%X\n", xex.xex_offset_to_va(file, rva));
printf("Virtual Addr: 0x%X\n", result);
}

if (!result)
{
printf("\nThe given address was unable to be converted, either:\n");
printf("- The given number is invalid\n");
printf("- The address is out-of-bounds\n");
printf("- The VA is part of a zero-compressed block in the file\n");
printf("(even with uncompressed XEXs, blocks of zeroes are removed from the file to save some space)\n");
}
}

Expand Down

0 comments on commit d9de36c

Please sign in to comment.