Move hexpatch to Rust

This commit is contained in:
topjohnwu
2023-06-20 18:17:26 -07:00
parent 5805573625
commit 399b9e5eba
13 changed files with 78 additions and 39 deletions
-1
View File
@@ -58,7 +58,6 @@ bool frm_rf(int dirfd);
} // extern "C"
using rust::fd_path;
int fd_pathat(int dirfd, const char *name, char *path, size_t size);
void mv_path(const char *src, const char *dest);
void mv_dir(int src, int dest);
+3
View File
@@ -6,3 +6,6 @@
#include "../logging.hpp"
#include "../missing.hpp"
#include "../base-rs.hpp"
using rust::xpipe2;
using rust::fd_path;
+5
View File
@@ -26,6 +26,11 @@ pub mod ffi {
Debug,
}
unsafe extern "C++" {
include!("misc.hpp");
fn mut_u8_patch(buf: &mut [u8], from: &[u8], to: &[u8]) -> Vec<usize>;
}
extern "Rust" {
fn log_with_rs(level: LogLevel, msg: &[u8]);
fn exit_on_error(b: bool);
+10 -2
View File
@@ -32,8 +32,8 @@ void byte_data::swap(byte_data &o) {
std::swap(_sz, o._sz);
}
vector<size_t> byte_data::patch(byte_view from, byte_view to) {
vector<size_t> v;
rust::Vec<size_t> byte_data::patch(byte_view from, byte_view to) {
rust::Vec<size_t> v;
if (_buf == nullptr)
return v;
auto p = _buf;
@@ -50,6 +50,14 @@ vector<size_t> byte_data::patch(byte_view from, byte_view to) {
return v;
}
rust::Vec<size_t> mut_u8_patch(
rust::Slice<uint8_t> buf,
rust::Slice<const uint8_t> from,
rust::Slice<const uint8_t> to) {
byte_data data(buf);
return data.patch(from, to);
}
int fork_dont_care() {
if (int pid = xfork()) {
waitpid(pid, nullptr, 0);
+7 -1
View File
@@ -6,6 +6,7 @@
#include <string_view>
#include <bitset>
#include <random>
#include <cxx.h>
#include "xwrap.hpp"
@@ -195,7 +196,7 @@ struct byte_data : public byte_view {
uint8_t *buf() { return _buf; }
void swap(byte_data &o);
std::vector<size_t> patch(byte_view from, byte_view to);
rust::Vec<size_t> patch(byte_view from, byte_view to);
};
template<size_t N>
@@ -217,6 +218,11 @@ struct heap_data : public byte_data {
friend byte_channel;
};
rust::Vec<size_t> mut_u8_patch(
rust::Slice<uint8_t> buf,
rust::Slice<const uint8_t> from,
rust::Slice<const uint8_t> to);
uint64_t parse_uint64_hex(std::string_view s);
int parse_int(std::string_view s);
+12
View File
@@ -9,6 +9,8 @@ use std::{fmt, io, slice, str};
use libc::c_char;
use thiserror::Error;
use crate::ffi;
pub fn copy_str<T: AsRef<[u8]>>(dest: &mut [u8], src: T) -> usize {
let src = src.as_ref();
let len = min(src.len(), dest.len() - 1);
@@ -346,3 +348,13 @@ impl<T> LibcReturn for *mut T {
self.is_null()
}
}
pub trait MutBytesExt {
fn patch(&mut self, from: &[u8], to: &[u8]) -> Vec<usize>;
}
impl<T: AsMut<[u8]>> MutBytesExt for T {
fn patch(&mut self, from: &[u8], to: &[u8]) -> Vec<usize> {
ffi::mut_u8_patch(self.as_mut(), from, to)
}
}
-4
View File
@@ -5,10 +5,6 @@
#include <poll.h>
#include <fcntl.h>
#include "base-rs.hpp"
using rust::xpipe2;
extern "C" {
FILE *xfopen(const char *pathname, const char *mode);