Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ See [workspace integration](docs/zipview-workspace.md) for Git dependencies, bui
its exact length, the bytes after the last file's packed data (quick open data, end of archive header, padding) exactly
as rar wrote them, and whether that is certain, so a rebuilt volume passes par2; see
[RAR 5.0 volume tails](docs/rar5-volume-tail.md).
- Placing and framing a dead RAR 5.0 volume (`zipfast::rar5::plan_dead_volumes`, `frame_dead_volume`): where
each dead volume's part lies in a stored member and which sibling lends its headers (with the volume number width
shift and the par2 last-volume rule), then a head and tail exact as rar wrote them or a virtual frame without the
part CRC32, read back before use, with every refusal typed; see [dead RAR 5.0 volumes](docs/rar5-dead-volume.md).
- Rebuilding a lost RAR 4.x volume end (`zipfast::rar5::rebuild_rar4_volume_tail`): the 20-byte end of archive
block after the last file's data, with its CRC32 of the volume's bytes and its volume number, exactly as rar wrote
it; see [RAR 4.x volume tails](docs/rar4-volume-tail.md).
Expand Down
172 changes: 172 additions & 0 deletions docs/rar5-dead-volume.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Placing and framing a dead RAR 5.0 volume

A RAR set stores one large member without compression, and one volume is dead: nothing of it arrived, not even its
headers. A downloader can borrow the member bytes that volume held from a verified duplicate, and read the volume as
a virtual one: a head rebuilt from a readable sibling, then the borrowed bytes, then a rebuilt tail. The archive
files on disk are never changed. `zipfast::rar5` does this in two steps:

```rust
use zipfast::rar5::{DeadVolume, FrameInput, MemberInfo, PlanOptions, ReadableVolume, VolumeState};
use zipfast::rar5::{frame_dead_volume, plan_dead_volumes};

let plans = plan_dead_volumes(&states, &member, &PlanOptions::default());
if let Some(stop) = plans.stop {
log::info!("Dead volumes in {first} cannot be placed yet: {stop}");
}
for plan in &plans.plans {
let (head, tail) = template_bytes(plan.template, &plan.template_layout); // bytes before and after its part
let input = FrameInput { template_head: &head, template_tail: &tail, earlier_parts_complete, borrowed_len };
let frame = frame_dead_volume(plan, &member, &input)?;
// the volume reads as frame.head, then frame.part_len borrowed bytes at frame.part_offset, then frame.tail
}
```

## API

- `plan_dead_volumes(&[VolumeState], &MemberInfo, &PlanOptions) -> DeadVolumePlans { plans, stop }`. It plans the
dead volumes in set order and stops at the first problem. The plans made before that problem are kept.
- `frame_dead_volume(&DeadVolumePlan, &MemberInfo, &FrameInput) -> Result<DeadVolumeFrame, FrameRefusal>` builds
one plan's frame and reads it back.
- `whole_file_crc32(&[(crc32, len)]) -> u32` combines the parts' CRC32s, in order, into the whole file's CRC32. A
dead last volume's file header records that value.
- `DeadVolumePlan::with_part_crc(crc)` adds a CRC32 learned after planning. A plan that was Virtual only because the
CRC32 was missing becomes Exact when the exact length is known.

Inputs:

| type | fields |
|---|---|
| `VolumeState` | `Readable(ReadableVolume)`, `Dead(DeadVolume)` or `Unknown` (not downloaded yet, or unreadable), one per volume in set order. The index comes from the volume name: `part01` is 0 |
| `ReadableVolume` | `version` (`RarFormat::Rar5` or `Rar4`), `len`, `part_start`, `part_len`, `split_before`, `split_after`, `framing_intact` (the bytes before and after the part are all present and sound) |
| `DeadVolume` | `nzb_size` (yEnc-encoded, when listed), `exact_len` (from par2; required for the last volume), `part_crc` (the CRC32 its file header records: the part's for a middle volume, the whole file's for the last) |
| `MemberInfo` | `name`, `unpacked_size`, and the flags that make a set unusable: `headers_encrypted`, `encrypted`, `compressed`, `blake2sp` |
| `PlanOptions` | `prefer_exact` (default true) |
| `FrameInput` | `template_head` and `template_tail` (the template's bytes before and after its part), `earlier_parts_complete` (needed for the last volume), `borrowed_len` (checked against the plan when it is given) |

A `DeadVolumePlan` gives:

- `index` (the dead volume) and `template` (the sibling that lends its bytes), plus `template_layout` (the
template's own numbers, which its bytes must match);
- `member_offset` and `part_len`;
- `volume_len`, `head_len` and `tail_len`: the dead volume's layout as rar wrote it;
- `width_shift`;
- `last` and `last_fit` (`Bare` or `Framed`, for the last volume);
- `exact_len` and `part_crc`;
- `mode` and `reason`.

## Placement rules

- The first volume must be readable. It is never rebuilt.
- The member offset of a dead volume is the sum of the part lengths before it. An `Unknown` volume before a dead one
stops planning (`NotReadable`). An `Unknown` volume after the last dead one does not.
- The template is the nearest readable middle volume before the dead one and the nearest after it. A middle volume
is neither the first nor the last of the set, and its part continues both ways. If both exist, their layouts must
agree. A sibling whose RAR 5.0 volume number has the dead volume's width is preferred. Rar writes the number as a
variable-length integer, so the width grows at indices 128 and 16384. Otherwise the nearer sibling wins, then the
one before. A sibling across a width boundary is shifted by the difference: the part starts that many bytes later
and is that much shorter, and the volume keeps its length. RAR 4.x has no shift.
- If the template's framing is damaged (`framing_intact: false`), an identical sibling with intact framing lends the
bytes instead. Identical means the same length, part start and part length.
- **Middle volume.** It takes the template's length and its (shifted) part length. The NZB size must lie in
`[len, len + len/16]`, and a par2 length, when given, must equal `len`.
- **Last volume (RAR 5.0 only).** Its part is what is left of the member, and no more than a middle volume's part
(`LastTooLarge`). The par2 length must fit `rar5_last_volume_len(head_len, part_len, template tail)`
([uniform volumes](uniform-volumes.md)). That is either the padded "framed" length or the "bare" length, which
ends at the end block. Without that proof, a member stored after the part would be lost without a word. The NZB
size gets a further 1024 bytes of slack.
- **The parts must add up.** When a readable volume closes the member, the parts up to it (dead ones included) must
equal the member's size, and they may never exceed it. Otherwise planning stops with `MemberSizeMismatch` and drops
every plan made so far. Past an `Unknown` volume the sum cannot be checked, so the plans before it rest on the
parts read so far.
- While the member is open, every readable volume after the first must continue it (split-before), including the
one right after a dead volume.
- RAR 1.5 to 4.x last volumes are refused, because their part must carry the whole file's checksum. A dead volume
that would close the member while it is not the listed last volume, or that comes after the member ended, is
refused (`MayBeLast`).

## Modes and reasons

| mode | when | frame |
|---|---|---|
| `Exact` | `prefer_exact`, and both the part CRC32 and the exact length are known (`PartCrcAndExactLength`) | The volume byte for byte as rar wrote it, quick open record and padding included, from [`rebuild_volume_headers`](rar5-volume-headers.md). Par2 verifies it |
| `Virtual` | `VirtualRequested`, `NoPartCrc`, `NoExactLength`, or (in a frame) `ExactRefused(ExactRefusal)` | The main header with the sibling's archive flags and the dead volume's number, without its extra area (the locator of the sibling's quick open and recovery data). Other headers in front of the file are copied unchanged. The file header gets split-before, split-after unless last, and the new data size. Its CRC32 is the given one, or none: the flag is cleared and the four bytes are dropped. Behind the part comes only the end header, with "more volumes" cleared for the last volume. Every header CRC32 is recomputed |

A middle part normally goes Virtual without a CRC32. The volume can then be handed to the extractor before every
borrowed byte has arrived. Ranges still missing read as holes and are patched later, and the head never changes.
When an exact rebuild is refused, its layout differs from the plan's, or it does not read back, `frame_dead_volume`
falls back to Virtual and reports `ExactRefused(kind)` (`PlanDiffers`, `ReadBack` or the rebuild's own refusal).

## What is verified, and what the caller vouches for

Every frame is read back through zipfast's own RAR 5.0 header reader (`VolumeMapBuilder`), with zeros standing in
for the part. The read must:

- pass every header CRC32, and reach the end header;
- give the dead volume's number and the right "more volumes" flag;
- list exactly one file, with the member's full name and unpacked size;
- show that file stored, not encrypted, not a directory;
- show it split-before, and split-after exactly when the volume is not the last;
- show the planned part length, starting right after the head;
- record the planned CRC32, or none.

**The part bytes are never seen.** The caller vouches for the borrowed bytes: that they come from a verified
duplicate of the same member, at `member_offset`, `part_len` long. zipfast verifies only the frame. A middle Virtual
frame carries no part CRC32, so wrong borrowed bytes surface only at the member's final CRC32, in the real last
volume. A dead last volume is framed only when `earlier_parts_complete` is set and the plan carries the whole file's
CRC32 (`whole_file_crc32` over every part, in order).

## Refusals

`PlanStop`:

| stop | when |
|---|---|
| `Unusable(EncryptedHeaders \| EncryptedMember \| Compressed \| NotCrc32)` | the set cannot be borrowed for: nothing is planned |
| `EmptySet`, `FirstNotReadable` | no volumes, or volume 1 is not readable |
| `NotReadable { index, found }` | a volume before a dead one is not on disk or not readable yet |
| `NoTemplate { index, cause: NoSibling \| Disagree { before, after } \| FramingDamaged }` | no readable sibling gives the layout |
| `MayBeLast { index }` | it would close the member but is not the listed last volume, or the member already ended |
| `LastNeedsPar2Len`, `LastLenMismatch { par2_len, framed, bare }` | the last volume without a fitting par2 length |
| `NzbSizeMisfit { listed, expected }`, `ExactLenMisfit { exact, expected }` | the listed size does not fit a volume like its sibling |
| `PastMemberEnd { end, member_size }` | it would end past the member's end |
| `MoreThanMember { index }` | the readable volumes up to it hold more than the member, before any dead volume was planned |
| `MemberSizeMismatch { index, parts, member_size }` | the parts, dead ones included, add up to more than the member, or to less when a readable volume closes it. Every plan made before is dropped: their placements rest on those sums |
| `LastTooLarge { index, part_len, max }` | a dead last volume would hold more than a middle volume's part: rar never writes one larger, so volumes are missing |
| `Rar4LastVolume`, `MixedVersions` | a RAR 1.5-4.x last volume, or volumes of two formats |
| `Inconsistent { index, what }` | impossible readable values: a part past its volume's end, a first volume continuing a member, a later volume not continuing it while it is open, overflowing sums |

`FrameRefusal`:

| refusal | when |
|---|---|
| `UnsupportedVersion` | not RAR 5.0 |
| `TemplateChanged` | the template's bytes are not the lengths its plan recorded, or its file header's data size differs: the sibling changed |
| `Template(TemplateFault)` | `NoSignature`, `Truncated`, `Corrupt`, `EncryptedHeaders`, `NoMainHeader`, `MainHeaderNoVolumeNumber`, `MainHeaderWithData`, `VolumeNumber` (not the template's index), `DataBeforeFile`, `NoFileHeader`, `FileHeaderNotAtPart`, `NotAFile`, `CompressedPart`, `EncryptedPart`, `NotCrc32`, `Redirection` (a symbolic or hard link record), `FileAfterPart`, `NoEndHeader` |
| `LastVolumeIncomplete`, `LastVolumeNeedsCrc` | a last volume before every earlier part is complete, or without the whole file's CRC32 |
| `BorrowedLengthDiffers { planned, given }` | the borrowed data's length is not the plan's |
| `Overflow` | the frame's length overflows |
| `ReadBack(DoesNotRead \| NoFile \| OtherFile \| NotStored \| SplitFlags \| PartPlacement \| VolumeFacts \| Crc)` | the rebuilt headers do not read back as planned |

Whether a member is a streamable video or a nested archive worth borrowing for is the caller's policy. It is not
checked here.

## Hostile input

All arithmetic on readable values, sizes and template fields is checked. Allocation is bounded by the template's
actual head and tail bytes. Nothing panics on any input. `tests/rar5_dead_volume.rs` drives the planner with 20,000
random sets of extreme values, and flips three bit patterns at every byte of a real template's head and tail, and
cuts its head at every length.

## Checked against

`tests/rar5_dead_volume.rs`:

- **Real set** (`tests/fixtures/rar5-dead-volume/stored`, rar 7.30 `-m0 -v20k`, with quick open records). Each
middle volume is deleted in turn and rebuilt. Exact frames match the original volume byte for byte. Virtual
frames, with and without the CRC32, extract byte-identical with zipfast's extractor. The last volume is rebuilt
exactly from its par2 length and the combined CRC32, and virtually, and the par2 length rules are checked. UnRAR
7.30 also tests the virtual sets OK.
- **Width shift.** A synthetic 134-volume set has rar's fixed volume size, so the part is one byte shorter from index
128 on. Volume 128 is rebuilt from 127 (+1) and from 129 (same width). Volumes 1 to 127, all dead, are rebuilt from
index 128 (-1), exactly and virtually, and the set extracts.
- **Every refusal.** Each `PlanStop`, `TemplateFault` and the other frame refusals are reached at least once.
Loading
Loading