#! @file msf.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the writer of the file a PDB file's streams are held in, an MSF #! file of version 7: blocks of 4096 bytes, the first describing the file, the #! second and third, and those 4096 blocks after them, the map of which blocks #! are free, then each stream's blocks in turn, then the directory naming each #! stream's size and blocks, and a block naming the directory's. And the table #! of strings a PDB file names files and other text by. import std::containers; import std::io; module binary::pdb; #! The bytes of a block. const block_bytes: u64 = 4096; #! Whether a block holds a map of free blocks, as every block 1 and 2 past a #! multiple of 4096 does. #! @param block The block. #! @returns Whether it does. def holds_free_map(block: u64): bool { val within := block % block_bytes; return within == 1 || within == 2; } #! Lays streams out in an MSF file, giving the file, and where the first byte #! of each stream's first block is, or 0 for one holding nothing. #! @param streams The streams, by number. #! @param firsts Where the first byte of each is, filled. #! @returns The file. pub def msf_file(streams: &vector, firsts: &mut vector): byte_writer { # Blocks are given out in order, past the maps of free blocks. var next: u64 = 3; var blocks := vector::::new(); # Each stream's blocks, one after another. var first_block := vector::::new(); # Where each stream's are in `blocks`. firsts.clear(); for &each in streams.span { first_block.append(blocks.length); val count := (each.length + block_bytes - 1) / block_bytes; firsts.append(0); for _ in 0..count { while holds_free_map(next) { next++; } blocks.append(next); next++; } } first_block.append(blocks.length); # The directory: how many streams, each one's size, then each one's blocks. var directory := byte_writer::new(); directory.u32(streams.length); for &each in streams.span { directory.u32(each.length); } for &each in blocks.span { directory.u32(each); } var directory_blocks := vector::::new(); val directory_count := (directory.length + block_bytes - 1) / block_bytes; for _ in 0..directory_count { while holds_free_map(next) { next++; } directory_blocks.append(next); next++; } while holds_free_map(next) { next++; } val map_block := next; next++; val total := next; var file := byte_writer::new(); file.zeros(total * block_bytes); # The file's description: its signature, the size of its blocks, the map of # free blocks in use, how many blocks there are, the directory's bytes, and # the block naming the directory's blocks. val magic: strview = "Microsoft C/C++ MSF 7.00\r\n"; for i in 0..magic.length { file.set_byte(i, magic[i]); } file.set_byte(26, 0x1A); file.set_byte(27, 'D' as u8); file.set_byte(28, 'S' as u8); file.set_u32(32, block_bytes); file.set_u32(36, 1); file.set_u32(40, total); file.set_u32(44, directory.length); file.set_u32(48, 0); file.set_u32(52, map_block); # Every map of free blocks says every block is free, but those the file # holds, which the map in use, the one in block 1 and those 4096 after it, # says are taken, a bit of each, the first block's lowest. var b: u64 = 1; while b < total { if holds_free_map(b) { for k in 0..block_bytes { file.set_byte(b * block_bytes + k, 0xFF); } } b++; } for block in 0..total { val map := 1 + (block / (8 * block_bytes)) * block_bytes; val byte := map * block_bytes + (block % (8 * block_bytes)) / 8; val bit := block % 8; file.set_byte(byte, file.get_byte(byte) & ~((1u << bit) as u8)); } # Each stream's bytes, in its blocks. for s in 0..streams.length { val data := streams[s].span; var at: u64 = 0; for k in first_block[s]..first_block[s + 1] { val block := blocks[k]; if at == 0 { firsts[s] = block * block_bytes; } val take := data.length - at < block_bytes ? data.length - at : block_bytes; memcpy(file.slice.data + block * block_bytes, data.data + at, take); at += take; } } var at: u64 = 0; val directory_data := directory.span; for &block in directory_blocks.span { val take := directory_data.length - at < block_bytes ? directory_data.length - at : block_bytes; memcpy(file.slice.data + block * block_bytes, directory_data.data + at, take); at += take; } for k in 0..directory_blocks.length { file.set_u32(map_block * block_bytes + 4 * k, directory_blocks[k]); } return file; } # The table of names. #! A table of strings, as the stream `/names` holds them: each string once, #! ending with a zero, after the empty one, and a table of their hashes a #! reader finds one by. pub struct name_table { var bytes: byte_writer; #! The strings. var held: interner; #! The strings, by ID. var at: vector; #! Where each is in the table, by its ID. } #! Makes a table holding only the empty string. #! @returns The table. pub def name_table::new(): name_table { var made := name_table { bytes = byte_writer::new(), held = interner::::new(), at = vector::::new() }; made.bytes.byte(0); return made; } #! Where a string is in the table, adding it where it is new. #! @param this The table. #! @param text The string. #! @returns Its offset. pub def name_table::offset_of(&mut this, text: strview): u32 { return 0 if text.length == 0; val id := this.held.intern_view(text); if id < this.at.length { return this.at[id]; } val at := this.bytes.length as u32; this.bytes.put_terminated(text); this.at.append(at); return at; } #! The table as a stream holds it: its signature, the version of its hash, the #! strings' bytes, then the strings' places by their hashes, each in the first #! free bucket from its hash's on, then how many strings there are. The #! buckets grow as Microsoft's tools grow them, so three of four at most hold a #! string. #! @param this The table. #! @returns The stream. pub def name_table::stream(&this): byte_writer { val count := this.at.length; var buckets: u64 = 1; for k in 1..count + 1 { if buckets * 3 / 4 < k { buckets = buckets * 3 / 2 + 1; } } var slots := vector::::new(); slots.resize(buckets); val all := this.bytes.span; for &offset in this.at.span { var end := offset as u64; while all[end] != 0 { end++; } val text := span::::over(all.data + offset as u64, end - offset as u64); var slot := hash_v1(text) as u64 % buckets; while slots[slot] != 0 { slot = (slot + 1) % buckets; } slots[slot] = offset; } var made := byte_writer::new(); made.u32(0xEFFEEFFE); made.u32(1); made.u32(this.bytes.length); made.put(all); made.u32(buckets); for &each in slots.span { made.u32(each); } made.u32(count); return made; }