Skip to main content

serialize_header_preserving

Function serialize_header_preserving 

Source
pub fn serialize_header_preserving(h: &Header, original: &[u8; 16]) -> [u8; 16]
Expand description

Write the edits in h over original, the 16 header bytes h was parsed from, and return the result.

Only the bits of fields whose value differs from parse_header(original) are rewritten; every other bit of original comes back unchanged. So an unedited header round-trips byte for byte, whatever it holds – exponent-notation sizes, reserved bits, reserved Vs. PPU nibbles, and the junk some dumpers left in bytes 8-15 of iNES 1.0 headers. This is the public header writer (since v2.9.8 the only one) and what the header editor writes to disk.

One edit reaches outside its own field: on an iNES 1.0 header, setting a mapper of 16 or more zeroes a non-zero tail in bytes 12-15, which would otherwise mask the new mapper’s bits 4-7 (the "DiskDude!" rule).

An edited field is written in its canonical encoding: a size in the standard notation when that can express it and in exponent-multiplier notation otherwise, a RAM size as its 64 << shift nibble. Toggling is_nes2 changes what bytes 7-15 mean, so that edit re-encodes the whole header canonically, which writes every reserved bit as zero. So does an original that does not parse. To build a header from nothing, pass the 16 bytes of an empty iNES 1.0 header ("NES\x1A" and twelve zeros) as original.