Skip to content

Refactor: Read the archive and squash offsets as members - #7650

Merged
ozgb merged 1 commit into
HectorBailey:mainfrom
giveen:issue-7078-4
Nov 14, 2026
Merged

ozgb merged 1 commit into
HectorBailey:mainfrom
giveen:issue-7078-4

Conversation

@giveen

@giveen giveen commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Part of #7078

Model: Ares / claude-opus-5

Rewrites the 7 byte-offset accesses in src/util that the issue counts, and 2 more it does not see because their offset is a variable. tools/rewrite_offsets.py --dry-run now reports 0 rewrites for all four files.

Adds src/util/archive_directory.h, one ArchiveDirectory view (int count; ArchiveEntry* entries;), and includes it in the four util/ files that each carried an identical copy: hpi_4be320.cpp, hpi_4bb4e0.cpp, hpi_4bc800.cpp and hpi_4bd830.cpp. That is the rule-2 fold for this folder.

The name is evidence-based: data/thaldren/structs.csv has Thaldrens HPI_DirectoryEntry for the sibling 9-byte entry record, which src/util/archive_entry.h already names ArchiveEntry, and the directory header is the block that count and offset live in. count is the entry count and entries the offset of the first one from the archive base, which is what the rewrites show: the count is multiplied by 9 (the entry size) to size the new block, and read back as the loop bound.

Two accesses the issue does not count: *(unsigned int*)(base + root) = 0 and ++(*(unsigned int*)(base + root)) in 0x4bd3b0. The issue counts (char*)p + <constant>, so a variable offset is invisible to it, but they are the same count field as the constant-offset read three lines below, and leaving them would have kept the field half-named.

Checks: checkall 95 of 95 MATCH (every function in hpi.cpp, hpi_4be320.cpp, hpi_4bb4e0.cpp, hpi_4bc800.cpp, hpi_4bd830.cpp, sqsh.cpp and registry.cpp); place.py MD5 8e74a1dffa1f5988624c52048f5b20cd, with and without --no-orig --strict; modules.py --check passes; cleanup_progress.py byte-offset count 62 to 53

Left out:

  • the ArchiveDirectory copy in hpi.cpp stays local, with the reason at its declaration. Folding it too costs that file 4 symbol ids and drops 0x4bb2e0 (HAPI_OpenArchive) to 94.5% and 0x4bd160 (HAPI_CloseArchive) to 99.1%. tools/idpad.py finds no set of real unused declarations that brings them back, and adding a fake one is what the rule forbids. Four files share the header, so the duplicate is down from five copies to two.
  • *(unsigned int*)(base + 8) in 0x4bd830, the archives own header. Nothing in src/ declares that block and data/thaldren/structs.csv has no name for it, so there is no member to write; it is the only access in the folder with no struct to point at.
  • *(char*)(a + 1) in 0x4b6820 (CompareByte1Equal) stays. The tool cannot rewrite it and neither can I: the function has no callers in src/, so its parameters keep the int that data/progress.csv records for the decorated name, and int + 1 advances 4 bytes. Changing the parameters to char* to get a[1] would change the arithmetic.

Suspected original bugs:

  • 0x4b6820 (CompareByte1Equal): it compares only the length byte, so it reports two registry values equal when their bytes differ. Nothing in src/ calls it, so I cannot tell whether the callers wanted a length-only test; the name says otherwise.

@ozgb
ozgb merged commit 679a75b into HectorBailey:main Nov 14, 2026
1 of 2 checks passed
@ozgb

ozgb commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Merged, thank you giveen, and welcome back! All 95 functions in the seven touched files MATCH, and place.py gives the shipped MD5 both with the original exe and with --no-orig --strict. The ArchiveDirectory header and the note on why hpi.cpp keeps its own copy are just right.

A few small things for a later pass, none of them blocking:

  • The "Left out" list says CompareByte1Equal (0x4b6820) stays, but the diff does rewrite it to ca[1]/cb[1] through local char*s. That's fine and still matches, so the description just needs to say so.
  • hpi.cpp gained a stray second blank line after its ArchiveDirectory.
  • In hpi_4bd830.cpp, (int)((ArchiveDirectory*)(off + base))->entries reads a little awkwardly. If a local ArchiveDirectory* dir keeps the match, it would be easier on the eye.

This branch is waiting to be deployed

1 waiting deployment
fork-ci — 4d4b23b4 Waiting Nov 14, 2026 by giveen via build #3573
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants