# What the tests share: reading words of bytes, and reading a stream back out # of an MSF file, as a debugger does. import std::io; import binary::pdb; import std::testing; # A word of bytes, least significant byte first. def word(bytes: span, at: u64, size: u64): u64 { var made: u64 = 0; for k in 0..size { made = made | (bytes[at + k] as u64 << (8 * k)); } return made; } # Whether bytes hold text at a place. def holds_at(bytes: span, at: u64, text: strview): bool { return false if at + text.length > bytes.length; for k in 0..text.length { return false if bytes[at + k] != text[k]; } return true; } # The bytes as hex, as the tests write what they expect. def hex_of(bytes: span): string { val digits: strview = "0123456789abcdef"; var made := string::new(nothing {}); for &each in bytes { made.append(digits.substr((each >> 4) as u64, 1)); made.append(digits.substr((each & 15) as u64, 1)); } return made; } # How many streams an MSF file holds. def stream_count(file: span): u64 { val directory := directory_of(file); return word(directory.view(), 0, 4); } # The directory of an MSF file, gathered from the blocks the block map names. def directory_of(file: span): byte_writer { val block := word(file, 32, 4); val size := word(file, 44, 4); val map := word(file, 52, 4); var made := byte_writer::new(); var left := size; var k: u64 = 0; while left > 0 { val at := word(file, map * block + 4 * k, 4) * block; val take := left < block ? left : block; for i in 0..take { made.byte(file[at + i]); } left -= take; k++; } return made; } # A stream of an MSF file, gathered from its blocks. def stream_of(file: span, number: u64): byte_writer { val block := word(file, 32, 4); val directory := directory_of(file); val names := directory.view(); val count := word(names, 0, 4); var at := 4 + 4 * count; for s in 0..number { val size := word(names, 4 + 4 * s, 4); at += 4 * ((size + block - 1) / block); } val size := word(names, 4 + 4 * number, 4); var made := byte_writer::new(); var left := size; var k: u64 = 0; while left > 0 { val from := word(names, at + 4 * k, 4) * block; val take := left < block ? left : block; for i in 0..take { made.byte(file[from + i]); } left -= take; k++; } return made; }