Files
mandy/docs/rpf.md
Leonardo Guimaraes 60c624ec9b tools/imhex: ImHex patterns for the RPF9 header and RBF0 records
rpf9.hexpat names the measured header fields (magic, data offset at +4;
the u32s at +8 and +12 unnamed) and shows the rest of the header and the
TOC as one opaque range; it decodes nothing there. rbf0.hexpat follows
the grammar in docs/rbf0.md section 3: 1-byte descriptor index and type,
all record types including 0x50, 8- or 4-byte values from the prologue,
and it stops where the root closes, as rbf0_index.parse_rbf0 does.

tests/test_imhex_patterns.py builds synthetic wide and narrow RBF0
streams and RPF9 headers, checks them with parse_rbf0, and when ImHex
is installed runs each pattern through `imhex --pl format -f json` and
compares the result with the parser's.
2026-11-22 11:07:12 -03:00

99 lines
5.7 KiB
Markdown

# RPF9 archives and the carve tools
GTA VI ships its assets in RPF9 archives. The **table of contents** (names, sizes, offsets and flags) is
encrypted/packed; this project does not decode it and does not search for keys. The **data area** is mostly
zlib streams laid end to end, so it can be measured and carved without the TOC. Names in the recovery view
are offset-based (`recovered/...`), not the original paths.
## What is readable
- The header: magic `RPF9` (stored as `9FPR` little-endian), sizes/counts, and the data start at `u32 +4`.
The TOC area immediately after the header is high-entropy and opaque.
- The data area: zlib streams, Bink 2 video (`KB2[a-z]`) in `movies.rpf`, and raw AWC banks in `audio.rpf`.
- From the existing survey in [docs/design.md](design.md): most archives are 93-99% plain zlib
(`levels.rpf` 98.3%, about 1.1M files); `audio.rpf` is 99.9% raw AWC, not zlib; about half of
`story000.rpf` and `shared_minigame000.rpf` is non-zlib. Unread bytes are recorded as a range, never
guessed.
## Carving
```powershell
python -I tools/rpf9_carve.py <archive.rpf> <out_dir> [--start N] [--limit BYTES] [--index-only]
```
The walker starts at `--start` or the header's data offset, finds a zlib header (`78 01/5e/9c/da`) or a Bink 2
magic, inflates the stream, and continues after the bytes it actually consumed. `--index-only` records the
streams without writing the inflated files; `--limit` bounds a sample; `--start` starts at a known offset.
`--start` must be 0 or more and `--limit` at least 1 (decimal or `0x` hex). An out-of-range value stops with a
one-line error and exit code 2 before any output is written, rather than silently scanning everything or nothing.
`<out_dir>/index.json` contains:
- `summary`: archive, files, compressed/inflated bytes, Bink 2 bytes/files, zero padding, unread bytes, span;
- `files`: `off`, `stored`, `size`, `pre` (bytes before the stream), `head` (first 16 inflated bytes), and for
Bink 2 the `w`/`h`/`frames` fields.
The walker advances by `len(chunk)`, not by the 1 MiB read request. A short final slice used to make `p`
overshoot EOF, so `used` exceeded the real stream size and every trailing stream was skipped; the regression
test `tests/test_rpf9_carve.py` builds a two-stream archive to keep that fixed.
The carve is read-only and works on any `.rpf`, `.ttpf` or archive-like file. It does not decrypt the TOC, use
keys, or write anything outside the output directory.
## Recovery tools
`tools/rpf9dump` is a bulk exporter built on the Core library from
[breadbbbb/rpf9-explorer](https://github.com/breadbbbb/rpf9-explorer) (a `third_party` submodule; the
related-projects page records the licence boundary). It carves validated resources; it does
not decrypt the TOC either.
```powershell
dotnet build tools/rpf9dump/rpf9dump.csproj -c Release
rpf9dump ttpf <pool.ttpf> <out>
rpf9dump models <archive.rpf> <out>
rpf9dump textures <archive.rpf> <out>
rpf9dump audio <audio.rpf> <out>
rpf9dump xma <audio.rpf> <out>
```
`audio` writes the AWC banks plus `audio_manifest.json` (stream ids, codec, channels, rate, duration); `xma`
writes RIFF XMA2 WAVs that ffmpeg decodes. `models`/`textures` write OBJ/DDS plus a manifest. The tool's
`ArchiveReader` recovery names are offset-based, and its structured path validates XML/PSIN, models and
textures; the original RPF paths still require the missing executable/key.
`tools/manifest.py` is independent of RPF: it hashes a directory tree and verifies it later, with `--jobs` on
both paths. It is used to check exported assets without keeping a second copy.
`tools/rpf9_names.py` is the lightweight lookup pass: it walks the same data area, inflates a bounded prefix
of each zlib stream and indexes the measured `name.#dr`/`#ft`/`#dd` spellings with their archive offsets. It
does not decode a resource or need the TOC; see [resource_names.md](resource_names.md).
### Viewing a header in ImHex
[tools/imhex/rpf9.hexpat](../tools/imhex/rpf9.hexpat) is a pattern for the [ImHex](https://imhex.werwolv.net/) hex
editor. It names the magic and the data offset at `+4`, shows the u32s at `+8` and `+12` unnamed, and shows the range
from `+0x10` to the data offset (the rest of the header and the TOC) as one opaque byte array. It decodes nothing in
that range. `python -I tests/test_imhex_patterns.py` runs it on a synthetic header when ImHex is installed.
## Limits
- The TOC is the hard boundary. File names, original paths and the exact file boundaries come from the TOC,
so they are unavailable until the executable (or a documented key/schema) is part of the project. This
repository does not search for or distribute keys.
- Some archives are not plain zlib (`audio.rpf`, parts of `story000.rpf`/`shared_minigame000.rpf`); the
recovery tools handle the formats they know and report the rest as unread.
- Bink 2 video is carved as whole `.bk2` streams. FFmpeg builds generally expose only Bink 1; the package's
`Bink2Scarlett.dll` decodes Bink 2 (see the movie notes).
- Extracted assets are for local use with your own copy. Never commit or share them.
## See also
- [docs/design.md](design.md) - the asset-stage table, coverage numbers and the recovery approach.
- [docs/page_directory.md](page_directory.md) - the `0DRP` page directory layout, `q38` resource boundaries and
`tools/page_index.py`.
- [docs/story.md](story.md) - measured `story000`/`shared_minigame000` composition, nested RPF9 sub-archives and the `PSIN`/`RSC8` headers.
- [docs/restofworld.md](restofworld.md) - measured composition of the rest-of-world and script archives.
- `docs/audio.md` - AWC layout and the audio exporters, once the audio guide is merged.
- the related-projects page - the rpf9-explorer/PercyStar/SCARAB licence boundaries.