feat: 提供了修改功能
This commit is contained in:
@@ -21,6 +21,10 @@ pub enum AppError {
|
||||
/// 初始化打开文件失败
|
||||
#[error("初始化打开文件失败!: {0}")]
|
||||
InitOpenFileFailed(String),
|
||||
|
||||
/// 无法修改文件
|
||||
#[error("无法修改文件!: {0}")]
|
||||
CannotModifyFile(String),
|
||||
}
|
||||
|
||||
impl serde::Serialize for AppError {
|
||||
|
||||
@@ -17,14 +17,37 @@ impl AppState {
|
||||
/// 初始化打开文件
|
||||
pub fn init_open_file(&mut self, file_path: &str) -> Result<(), AppError> {
|
||||
self.file_path = Some(file_path.to_string());
|
||||
self.mmap = Some(file::mmap_mut_file(file_path)?);
|
||||
self.mmap = Some(file::mmap_file(file_path)?);
|
||||
// 读取PE格式来判断是否是64位
|
||||
let mmap: &Mmap = self.mmap.as_ref().unwrap();
|
||||
self.is_64_bit = Some(mmap.is_64_bit()?);
|
||||
Ok(())
|
||||
}
|
||||
/// 获取文件内存映射
|
||||
|
||||
/// 获取文件内存映射的引用
|
||||
pub fn get_mmap_ref(&self) -> Result<&Mmap, AppError> {
|
||||
self.mmap.as_ref().ok_or(AppError::NoFileOpened)
|
||||
}
|
||||
|
||||
/// 设置文件内存映射
|
||||
pub fn set_mmap(&mut self, mmap: Mmap) {
|
||||
self.mmap = Some(mmap);
|
||||
}
|
||||
|
||||
/// 在指定偏移处写入数据,此举会同步修改文件内容
|
||||
pub fn write_data(&mut self, offset: usize, data: &[u8]) -> Result<(), AppError> {
|
||||
// 1. 重新以可读可写打开文件
|
||||
let file_path = self.file_path.as_ref().ok_or(AppError::NoFileOpened)?;
|
||||
let mut rw_mmap = file::mmap_file_rw(file_path)?;
|
||||
// 2. 向内存映射中写入数据
|
||||
rw_mmap[offset..(offset + data.len())].copy_from_slice(data);
|
||||
rw_mmap.flush()?;
|
||||
|
||||
// 3. 重新打开文件
|
||||
self.mmap = Some(file::mmap_file(file_path)?);
|
||||
self.is_64_bit = Some(self.get_mmap_ref()?.is_64_bit()?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,7 +30,7 @@ pub fn set_complete(app: AppHandle) -> Result<(), AppError> {
|
||||
}
|
||||
|
||||
// 命令,打开文件
|
||||
#[tauri::command]
|
||||
#[tauri::command(async)]
|
||||
pub fn command_open_file(
|
||||
file_path: &str,
|
||||
app_state: State<'_, Mutex<AppState>>,
|
||||
@@ -43,7 +43,7 @@ pub fn command_open_file(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[tauri::command(async)]
|
||||
pub fn command_get_file_pe_node_tree_data(
|
||||
app_state: State<'_, Mutex<AppState>>,
|
||||
) -> Result<Vec<PeNodeTreeData>, AppError> {
|
||||
@@ -100,7 +100,7 @@ pub fn command_get_file_pe_node_tree_data(
|
||||
}
|
||||
|
||||
// 命令,获取DOS头数据
|
||||
#[tauri::command]
|
||||
#[tauri::command(async)]
|
||||
pub fn command_get_pe_data_dos_header(
|
||||
app_state: State<'_, Mutex<AppState>>,
|
||||
) -> Result<Value, AppError> {
|
||||
@@ -114,7 +114,7 @@ pub fn command_get_pe_data_dos_header(
|
||||
}
|
||||
|
||||
// 命令,获取NT头数据
|
||||
#[tauri::command]
|
||||
#[tauri::command(async)]
|
||||
pub fn command_get_pe_data_nt_header(
|
||||
app_state: State<'_, Mutex<AppState>>,
|
||||
) -> Result<Value, AppError> {
|
||||
@@ -129,7 +129,7 @@ pub fn command_get_pe_data_nt_header(
|
||||
}
|
||||
|
||||
// 命令,获取文件头数据
|
||||
#[tauri::command]
|
||||
#[tauri::command(async)]
|
||||
pub fn command_get_pe_data_file_header(app_state: State<'_, Mutex<AppState>>) -> Result<Value, AppError> {
|
||||
let app_state = app_state.lock().unwrap();
|
||||
let mmap = app_state.get_mmap_ref()?;
|
||||
@@ -142,7 +142,7 @@ pub fn command_get_pe_data_file_header(app_state: State<'_, Mutex<AppState>>) ->
|
||||
}
|
||||
|
||||
// 命令,获取可选头数据
|
||||
#[tauri::command]
|
||||
#[tauri::command(async)]
|
||||
pub fn command_get_pe_data_optional_header(app_state: State<'_, Mutex<AppState>>) -> Result<Value, AppError> {
|
||||
let app_state = app_state.lock().unwrap();
|
||||
let mmap = app_state.get_mmap_ref()?;
|
||||
@@ -157,7 +157,7 @@ pub fn command_get_pe_data_optional_header(app_state: State<'_, Mutex<AppState>>
|
||||
}
|
||||
|
||||
// 命令,获取节区头数据
|
||||
#[tauri::command]
|
||||
#[tauri::command(async)]
|
||||
pub fn command_get_pe_data_section_headers(app_state: State<'_, Mutex<AppState>>) -> Result<Value, AppError> {
|
||||
let app_state = app_state.lock().unwrap();
|
||||
let mmap = app_state.get_mmap_ref()?;
|
||||
@@ -169,3 +169,15 @@ pub fn command_get_pe_data_section_headers(app_state: State<'_, Mutex<AppState>>
|
||||
"base_offset": section_headers_offset,
|
||||
}))
|
||||
}
|
||||
|
||||
// 命令,修改偏移处数据
|
||||
#[tauri::command(async)]
|
||||
pub fn command_write_data(
|
||||
app_state: State<'_, Mutex<AppState>>,
|
||||
offset: usize,
|
||||
data: Vec<u8>,
|
||||
) -> Result<(), AppError> {
|
||||
let mut app_state = app_state.lock().unwrap();
|
||||
app_state.write_data(offset, &data)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -15,6 +15,7 @@ pub fn run() {
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::set_complete,
|
||||
commands::command_open_file,
|
||||
commands::command_get_file_pe_node_tree_data,
|
||||
commands::command_get_pe_data_dos_header,
|
||||
@@ -22,7 +23,7 @@ pub fn run() {
|
||||
commands::command_get_pe_data_file_header,
|
||||
commands::command_get_pe_data_optional_header,
|
||||
commands::command_get_pe_data_section_headers,
|
||||
commands::set_complete,
|
||||
commands::command_write_data,
|
||||
])
|
||||
.setup(|app| {
|
||||
app.manage(Mutex::new(app_state::AppState::default()));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use memmap2::*;
|
||||
|
||||
/// 以只读的方式创建文件映射
|
||||
pub fn mmap_mut_file(file_path: &str) -> Result<Mmap, std::io::Error> {
|
||||
pub fn mmap_file(file_path: &str) -> Result<Mmap, std::io::Error> {
|
||||
let file = std::fs::OpenOptions::new().read(true).open(file_path)?;
|
||||
unsafe {
|
||||
MmapOptions::new()
|
||||
@@ -9,3 +9,13 @@ pub fn mmap_mut_file(file_path: &str) -> Result<Mmap, std::io::Error> {
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
/// 以读写方式创建文件映射
|
||||
pub fn mmap_file_rw(file_path: &str) -> Result<MmapMut, std::io::Error> {
|
||||
let file = std::fs::OpenOptions::new().read(true).write(true).open(file_path)?;
|
||||
unsafe {
|
||||
MmapOptions::new()
|
||||
.map_mut(&file)
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user