mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-10 20:24:35 -08:00
Use bytemuck
This commit is contained in:
@@ -15,3 +15,4 @@ libc = { workspace = true }
|
||||
cfg-if = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
argh = { workspace = true }
|
||||
bytemuck = { workspace = true }
|
||||
|
||||
@@ -10,12 +10,13 @@ use std::os::unix::fs::FileTypeExt;
|
||||
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd};
|
||||
use std::{io, mem, ptr, slice};
|
||||
|
||||
use bytemuck::{bytes_of_mut, Pod};
|
||||
use libc::{
|
||||
c_char, c_uint, dirent, mode_t, EEXIST, ENOENT, F_OK, O_CLOEXEC, O_PATH, O_RDONLY, O_RDWR,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
copy_cstr, cstr, errno, error, FlatData, FsPath, FsPathBuf, LibcReturn, Utf8CStr, Utf8CStrArr,
|
||||
copy_cstr, cstr, errno, error, FsPath, FsPathBuf, LibcReturn, Utf8CStr, Utf8CStrArr,
|
||||
Utf8CStrBuf,
|
||||
};
|
||||
|
||||
@@ -52,7 +53,7 @@ pub fn fd_path(fd: RawFd, buf: &mut dyn Utf8CStrBuf) -> io::Result<()> {
|
||||
|
||||
pub trait ReadExt {
|
||||
fn skip(&mut self, len: usize) -> io::Result<()>;
|
||||
fn read_flat_data<F: FlatData>(&mut self, data: &mut F) -> io::Result<()>;
|
||||
fn read_pod<F: Pod>(&mut self, data: &mut F) -> io::Result<()>;
|
||||
}
|
||||
|
||||
impl<T: Read> ReadExt for T {
|
||||
@@ -67,8 +68,8 @@ impl<T: Read> ReadExt for T {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_flat_data<F: FlatData>(&mut self, data: &mut F) -> io::Result<()> {
|
||||
self.read_exact(data.as_raw_bytes_mut())
|
||||
fn read_pod<F: Pod>(&mut self, data: &mut F) -> io::Result<()> {
|
||||
self.read_exact(bytes_of_mut(data))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::process::exit;
|
||||
use std::{io, mem, slice, str};
|
||||
use std::{io, slice, str};
|
||||
|
||||
use argh::EarlyExit;
|
||||
use libc::c_char;
|
||||
@@ -30,34 +30,6 @@ pub unsafe fn slice_from_ptr_mut<'a, T>(buf: *mut T, len: usize) -> &'a mut [T]
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FlatData
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn as_raw_bytes(&self) -> &[u8] {
|
||||
unsafe {
|
||||
let self_ptr = self as *const Self as *const u8;
|
||||
slice::from_raw_parts(self_ptr, mem::size_of::<Self>())
|
||||
}
|
||||
}
|
||||
fn as_raw_bytes_mut(&mut self) -> &mut [u8] {
|
||||
unsafe {
|
||||
let self_ptr = self as *mut Self as *mut u8;
|
||||
slice::from_raw_parts_mut(self_ptr, mem::size_of::<Self>())
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_size(&self) -> usize {
|
||||
mem::size_of::<Self>()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_flat_data {
|
||||
($($t:ty)*) => ($(impl FlatData for $t {})*)
|
||||
}
|
||||
|
||||
impl_flat_data!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
|
||||
|
||||
// Check libc return value and map to Result
|
||||
pub trait LibcReturn
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user