file signature check

This commit is contained in:
Theron 2020-03-01 17:47:43 -06:00
parent 24b752c2ee
commit 3845bb8ba1
2 changed files with 22 additions and 4 deletions

View File

@ -11,8 +11,11 @@ use cnrom::Cnrom;
use mmc3::Mmc3; use mmc3::Mmc3;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::Read; use std::io::Read;
use std::rc::Rc;
pub trait Mapper { pub trait Mapper {
fn read(&self, address: usize) -> u8; fn read(&self, address: usize) -> u8;
@ -108,6 +111,17 @@ impl Cartridge {
} }
pub fn check_signature(filename: &str) -> Result<(), String> {
let mut f = File::open(filename).map_err(|e| e.to_string())?;
let mut data = [0; 4];
f.read_exact(&mut data).map_err(|e| e.to_string())?;
if data == [0x4E, 0x45, 0x53, 0x1A] {
Ok(())
} else {
Err("file signature mismatch: not a valid iNES file".to_string())
}
}
/* /*
The mappings above are the fixed addresses from which the PPU uses to fetch data during rendering. The actual device that the PPU fetches data from, however, may be configured by the cartridge. The mappings above are the fixed addresses from which the PPU uses to fetch data during rendering. The actual device that the PPU fetches data from, however, may be configured by the cartridge.
$0000-1FFF is normally mapped by the cartridge to a CHR-ROM or CHR-RAM, often with a bank switching mechanism. $0000-1FFF is normally mapped by the cartridge to a CHR-ROM or CHR-RAM, often with a bank switching mechanism.

View File

@ -10,7 +10,7 @@ mod state;
use cpu::Cpu; use cpu::Cpu;
use ppu::Ppu; use ppu::Ppu;
use apu::Apu; use apu::Apu;
use cartridge::get_mapper; use cartridge::{check_signature, get_mapper};
use input::poll_buttons; use input::poll_buttons;
use screen::{init_window, draw_pixel, draw_to_window}; use screen::{init_window, draw_pixel, draw_to_window};
use state::{save_state, load_state, find_next_filename, find_last_filename}; use state::{save_state, load_state, find_next_filename, find_last_filename};
@ -203,8 +203,12 @@ fn process_events(event_pump: &mut EventPump, filepath: &PathBuf, cpu: &mut Cpu)
let res: Result<(), String> = load_state(cpu, &p) let res: Result<(), String> = load_state(cpu, &p)
.or_else(|e| {println!("{}", e); Ok(())}); .or_else(|e| {println!("{}", e); Ok(())});
res.unwrap(); res.unwrap();
} else if f.len() > 4 && &f[f.len()-4..] == ".nes" { // } else if f.len() > 4 && &f[f.len()-4..] == ".nes" {
return GameExitMode::NewGame(f) } else {
match cartridge::check_signature(&f) {
Ok(()) => return GameExitMode::NewGame(f),
Err(e) => println!("{}", e),
}
} }
}, },
_ => (), _ => (),