1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use std::iter::Peekable; use std::slice::Iter; use crate::{FromBytes, KOSValue, ToBytes}; use crate::errors::{ReadError, ReadResult}; use super::{instructions::Instr, symbols::KOSymbol, symbols::ReldEntry, SectionFromBytes}; use std::mem; pub trait SectionIndex { fn section_index(&self) -> usize; } #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum SectionKind { Null, SymTab, StrTab, Func, Data, Debug, Reld, Unknown, } impl From<u8> for SectionKind { fn from(byte: u8) -> Self { match byte { 0 => Self::Null, 1 => Self::SymTab, 2 => Self::StrTab, 3 => Self::Func, 4 => Self::Data, 5 => Self::Debug, 6 => Self::Reld, _ => Self::Unknown, } } } impl From<SectionKind> for u8 { fn from(kind: SectionKind) -> Self { match kind { SectionKind::Null => 0, SectionKind::SymTab => 1, SectionKind::StrTab => 2, SectionKind::Func => 3, SectionKind::Data => 4, SectionKind::Debug => 5, SectionKind::Reld => 6, SectionKind::Unknown => 255, } } } impl ToBytes for SectionKind { fn to_bytes(&self, buf: &mut Vec<u8>) { buf.push((*self).into()); } } impl FromBytes for SectionKind { fn from_bytes(source: &mut Peekable<Iter<u8>>, _debug: bool) -> ReadResult<Self> where Self: Sized, { let value = *source.next().ok_or(ReadError::SectionKindReadError)?; let kind = SectionKind::from(value); match kind { SectionKind::Unknown => Err(ReadError::UnknownSectionKindReadError(value)), _ => Ok(kind), } } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct SectionHeader { name_idx: usize, sh_kind: SectionKind, size: u32, } impl SectionHeader { pub fn null() -> Self { SectionHeader { name_idx: 0, sh_kind: SectionKind::Null, size: 0, } } pub fn new(name_idx: usize, sh_kind: SectionKind) -> Self { SectionHeader { name_idx, sh_kind, size: 0, } } pub fn set_name_idx(&mut self, name_idx: usize) { self.name_idx = name_idx; } pub fn name_idx(&self) -> usize { self.name_idx } pub fn set_size(&mut self, size: u32) { self.size = size; } pub fn size(&self) -> u32 { self.size } pub fn kind(&self) -> SectionKind { self.sh_kind } pub fn size_bytes() -> usize { 9 } } impl ToBytes for SectionHeader { fn to_bytes(&self, buf: &mut Vec<u8>) { (self.name_idx as u32).to_bytes(buf); self.sh_kind.to_bytes(buf); self.size.to_bytes(buf); } } impl FromBytes for SectionHeader { fn from_bytes(source: &mut Peekable<Iter<u8>>, debug: bool) -> ReadResult<Self> where Self: Sized, { let name_idx = u32::from_bytes(source, debug) .map_err(|_| ReadError::SectionHeaderConstantReadError("name index"))? as usize; let sh_kind = SectionKind::from_bytes(source, debug)?; let size = u32::from_bytes(source, debug) .map_err(|_| ReadError::SectionHeaderConstantReadError("size"))?; Ok(SectionHeader { name_idx, sh_kind, size, }) } } #[derive(Debug)] pub struct SymbolTable { symbols: Vec<KOSymbol>, size: usize, section_index: usize, } impl SectionIndex for SymbolTable { fn section_index(&self) -> usize { self.section_index } } impl SymbolTable { pub fn new(amount: usize, section_index: usize) -> Self { SymbolTable { symbols: Vec::with_capacity(amount * mem::size_of::<KOSymbol>()), size: 0, section_index, } } pub fn get(&self, index: usize) -> Option<&KOSymbol> { self.symbols.get(index) } pub fn find_has_name(&self, name_idx: usize) -> Option<&KOSymbol> { self.symbols.iter().find(|sym| sym.name_idx() == name_idx) } pub fn position_by_name(&self, name_idx: usize) -> Option<usize> { self.symbols .iter() .position(|sym| sym.name_idx() == name_idx) } pub fn find(&self, symbol: &KOSymbol) -> Option<usize> { self.symbols.iter().position(|sym| sym == symbol) } pub fn add(&mut self, symbol: KOSymbol) -> usize { self.size += KOSymbol::size_bytes() as usize; self.symbols.push(symbol); self.symbols.len() - 1 } pub fn size(&self) -> u32 { self.size as u32 } pub fn symbols(&self) -> Iter<KOSymbol> { self.symbols.iter() } } impl SectionFromBytes for SymbolTable { fn from_bytes( source: &mut Peekable<Iter<u8>>, debug: bool, size: usize, section_index: usize, ) -> ReadResult<Self> { let num_symbols = size / KOSymbol::size_bytes() as usize; let mut read_symbols = 0; let mut sym_tab = SymbolTable::new(num_symbols as usize, section_index); while (read_symbols * KOSymbol::size_bytes() as usize) < size { let symbol = KOSymbol::from_bytes(source, debug)?; read_symbols += 1; sym_tab.add(symbol); } Ok(sym_tab) } } impl ToBytes for SymbolTable { fn to_bytes(&self, buf: &mut Vec<u8>) { for symbol in self.symbols.iter() { symbol.to_bytes(buf); } } } #[derive(Debug)] pub struct StringTable { hashes: Vec<u64>, contents: Vec<String>, section_index: usize, size: usize, } impl SectionIndex for StringTable { fn section_index(&self) -> usize { self.section_index } } impl StringTable { pub fn new(amount: usize, section_index: usize) -> Self { let mut contents = Vec::with_capacity(amount); contents.push(String::new()); StringTable { hashes: Vec::new(), contents, section_index, size: 1, } } pub fn get(&self, index: usize) -> Option<&String> { self.contents.get(index) } pub fn find(&self, s: &str) -> Option<usize> { let mut hasher = DefaultHasher::new(); hasher.write(s.as_bytes()); let hash = hasher.finish(); self.hashes.iter().position(|item| *item == hash) } pub fn strings(&self) -> Iter<String> { self.contents.iter() } pub fn add_checked(&mut self, new_str: &str) -> usize { match self.find(new_str) { Some(index) => index, None => self.add(new_str), } } pub fn add(&mut self, new_str: &str) -> usize { let mut hasher = DefaultHasher::new(); hasher.write(new_str.as_bytes()); let hash = hasher.finish(); self.hashes.push(hash); self.size += new_str.len() + 1; self.contents.push(String::from(new_str)); self.contents.len() - 1 } pub fn size(&self) -> u32 { self.size as u32 } } impl SectionFromBytes for StringTable { fn from_bytes( source: &mut Peekable<Iter<u8>>, debug: bool, size: usize, section_index: usize, ) -> ReadResult<Self> { let mut contents = Vec::new(); let mut hashes = Vec::new(); let mut read = 0; while read < size { let mut b = Vec::new(); while read < size { let c = *source.next().ok_or(ReadError::StringTableReadError)?; read += 1; if c == b'\0' { break; } b.push(c); } let s = String::from_utf8(b).map_err(|_| ReadError::StringTableReadError)?; let mut hasher = DefaultHasher::new(); hasher.write(s.as_bytes()); let hash = hasher.finish(); hashes.push(hash); contents.push(s); } if debug { println!("String table strings: "); for s in contents.iter() { println!("\t{}", s); } } Ok(StringTable { hashes, contents, section_index, size, }) } } impl ToBytes for StringTable { fn to_bytes(&self, buf: &mut Vec<u8>) { for string in self.contents.iter() { buf.extend_from_slice(string.as_bytes()); buf.push(0); } } } #[derive(Debug)] pub struct DataSection { hashes: Vec<u64>, data: Vec<KOSValue>, size: usize, section_index: usize, } impl SectionIndex for DataSection { fn section_index(&self) -> usize { self.section_index } } impl DataSection { pub fn new(amount: usize, section_index: usize) -> Self { DataSection { hashes: Vec::with_capacity(amount), data: Vec::with_capacity(amount), size: 0, section_index, } } pub fn find(&self, value: &KOSValue) -> Option<usize> { let mut hasher = DefaultHasher::new(); value.hash(&mut hasher); let hash = hasher.finish(); self.hashes.iter().position(|item| *item == hash) } pub fn add_checked(&mut self, value: KOSValue) -> usize { match self.find(&value) { Some(index) => index, None => self.add(value), } } pub fn add(&mut self, value: KOSValue) -> usize { self.size += value.size_bytes(); let mut hasher = DefaultHasher::new(); value.hash(&mut hasher); let hash = hasher.finish(); self.hashes.push(hash); self.data.push(value); self.data.len() - 1 } pub fn get(&self, index: usize) -> Option<&KOSValue> { self.data.get(index) } pub fn data(&self) -> Iter<KOSValue> { self.data.iter() } pub fn size(&self) -> u32 { self.size as u32 } } impl SectionFromBytes for DataSection { fn from_bytes( source: &mut Peekable<Iter<u8>>, debug: bool, size: usize, section_index: usize, ) -> ReadResult<Self> { let mut read = 0; let mut data = Vec::new(); let mut hashes = Vec::new(); while read < size { let kos_value = KOSValue::from_bytes(source, debug)?; read += kos_value.size_bytes(); let mut hasher = DefaultHasher::new(); kos_value.hash(&mut hasher); let hash = hasher.finish(); hashes.push(hash); data.push(kos_value); } if debug { println!("Data section:"); for d in data.iter() { println!("\t{:?}", d); } } Ok(DataSection { hashes, data, size, section_index, }) } } impl ToBytes for DataSection { fn to_bytes(&self, buf: &mut Vec<u8>) { for value in self.data.iter() { value.to_bytes(buf); } } } #[derive(Debug)] pub struct FuncSection { instructions: Vec<Instr>, size: usize, section_index: usize, } impl SectionIndex for FuncSection { fn section_index(&self) -> usize { self.section_index } } impl FuncSection { pub fn new(amount: usize, section_index: usize) -> Self { FuncSection { instructions: Vec::with_capacity(amount), size: 0, section_index, } } pub fn add(&mut self, instr: Instr) -> usize { let index = self.instructions.len(); self.size += instr.size_bytes(); self.instructions.push(instr); index } pub fn get(&self, index: usize) -> Option<&Instr> { self.instructions.get(index) } pub fn size(&self) -> u32 { self.size as u32 } pub fn instructions(&self) -> Iter<Instr> { self.instructions.iter() } } impl SectionFromBytes for FuncSection { fn from_bytes( source: &mut Peekable<Iter<u8>>, debug: bool, size: usize, section_index: usize, ) -> ReadResult<Self> { let mut new_size = 0; let mut instructions = Vec::new(); while new_size < size { let instr = Instr::from_bytes(source, debug)?; new_size += instr.size_bytes(); instructions.push(instr); } Ok(FuncSection { instructions, size, section_index, }) } } impl ToBytes for FuncSection { fn to_bytes(&self, buf: &mut Vec<u8>) { for instr in self.instructions.iter() { instr.to_bytes(buf); } } } #[derive(Debug)] pub struct ReldSection { entries: Vec<ReldEntry>, size: u32, section_index: usize, } impl SectionIndex for ReldSection { fn section_index(&self) -> usize { self.section_index } } impl ReldSection { pub fn new(amount: usize, section_index: usize) -> Self { ReldSection { entries: Vec::with_capacity(amount), size: 0, section_index, } } pub fn add(&mut self, entry: ReldEntry) -> usize { self.size += entry.size_bytes(); self.entries.push(entry); self.entries.len() - 1 } pub fn get(&self, index: usize) -> Option<&ReldEntry> { self.entries.get(index) } pub fn entries(&self) -> Iter<ReldEntry> { self.entries.iter() } pub fn size(&self) -> u32 { self.size } } impl ToBytes for ReldSection { fn to_bytes(&self, buf: &mut Vec<u8>) { for reld_entry in self.entries.iter() { reld_entry.to_bytes(buf); } } } impl SectionFromBytes for ReldSection { fn from_bytes( source: &mut Peekable<Iter<u8>>, debug: bool, size: usize, section_index: usize, ) -> ReadResult<Self> { let mut read = 0; let mut entries = Vec::new(); if debug { println!("Reld section:"); } while read < size { let entry = ReldEntry::from_bytes(source, debug)?; read += entry.size_bytes() as usize; if debug { println!("\t{:?}", entry); } entries.push(entry); } Ok(ReldSection { entries, size: size as u32, section_index, }) } } #[cfg(test)] mod tests { use super::*; use crate::kofile::symbols::{SymBind, SymType}; #[test] fn strtab_insert() { let mut strtab = StringTable::new(8, 0); let index = strtab.add(".text"); assert_eq!(index, 1); assert_eq!(strtab.get(index).unwrap(), ".text"); } #[test] fn strtab_get_null() { let strtab = StringTable::new(8, 0); assert_eq!(strtab.get(0).unwrap(), ""); } #[test] fn strtab_get_str() { let mut strtab = StringTable::new(8, 0); strtab.add("Hello"); assert_eq!(strtab.get(1).unwrap(), "Hello"); } #[test] fn strtab_get_str_2() { let mut strtab = StringTable::new(16, 0); strtab.add("Hello"); let index = strtab.add("world"); assert_eq!(index, 2); assert_eq!(strtab.get(index).unwrap(), "world"); } #[test] fn strtab_strings() { let mut strtab = StringTable::new(16, 0); strtab.add("Hello"); strtab.add("rust"); strtab.add("world"); let mut strs = strtab.strings(); assert_eq!(strs.next().unwrap(), ""); assert_eq!(strs.next().unwrap(), "Hello"); assert_eq!(strs.next().unwrap(), "rust"); assert_eq!(strs.next().unwrap(), "world"); } #[test] fn symtab_insert() { let mut strtab = StringTable::new(8, 0); let mut symtab = SymbolTable::new(1, 0); let s_index = strtab.add("fn_add"); let sym = KOSymbol::new(s_index, 0, 0, SymBind::Local, SymType::Func, 3); let sym_index = symtab.add(sym); assert_eq!(sym_index, 0); } #[test] fn symtab_get() { let mut strtab = StringTable::new(8, 0); let mut symtab = SymbolTable::new(1, 0); let s_index = strtab.add("fn_add"); let sym = KOSymbol::new(s_index, 0, 0, SymBind::Local, SymType::Func, 3); let sym_index = symtab.add(sym); assert_eq!( strtab .get(symtab.get(sym_index).unwrap().name_idx()) .unwrap(), "fn_add" ); } #[test] fn data_insert() { let mut data_section = DataSection::new(1, 0); let index = data_section.add(KOSValue::Int32(365)); assert_eq!(index, 0); } #[test] fn data_insert_2() { let mut data_section = DataSection::new(2, 0); data_section.add(KOSValue::ArgMarker); let index = data_section.add(KOSValue::Bool(true)); assert_eq!(index, 1); } #[test] fn data_get() { let mut data_section = DataSection::new(1, 0); let index = data_section.add(KOSValue::Int16(657)); assert_eq!(data_section.get(index).unwrap(), &KOSValue::Int16(657)); } #[test] fn write_header() { let mut sh = SectionHeader::new(0, SectionKind::SymTab); sh.set_size(42); let mut buf = Vec::new(); sh.to_bytes(&mut buf); assert_eq!( buf, vec![0x00, 0x00, 0x00, 0x00, 0x01, 0x2a, 0x00, 0x00, 0x00] ); } }