You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Move hexpatch to Rust
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -6,3 +6,6 @@
|
||||
#include "../logging.hpp"
|
||||
#include "../missing.hpp"
|
||||
#include "../base-rs.hpp"
|
||||
|
||||
using rust::xpipe2;
|
||||
using rust::fd_path;
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user