Magisk/native/src/base/misc.rs

137 lines
3.2 KiB
Rust
Raw Normal View History

2022-07-06 04:13:09 +00:00
use std::cmp::min;
2022-08-09 05:53:37 +00:00
use std::ffi::CStr;
2022-07-06 04:13:09 +00:00
use std::fmt::Arguments;
use std::{fmt, slice};
2022-07-06 04:13:09 +00:00
2023-05-02 23:49:43 +00:00
pub fn copy_str(dest: &mut [u8], src: &[u8]) -> usize {
let len = min(src.len(), dest.len() - 1);
dest[..len].copy_from_slice(&src[..len]);
dest[len] = b'\0';
len
}
2022-07-06 04:13:09 +00:00
struct BufFmtWriter<'a> {
buf: &'a mut [u8],
used: usize,
}
impl<'a> BufFmtWriter<'a> {
fn new(buf: &'a mut [u8]) -> Self {
BufFmtWriter { buf, used: 0 }
}
}
impl<'a> fmt::Write for BufFmtWriter<'a> {
// The buffer should always be null terminated
fn write_str(&mut self, s: &str) -> fmt::Result {
if self.used >= self.buf.len() - 1 {
// Silent truncate
return Ok(());
}
2023-05-02 23:49:43 +00:00
self.used += copy_str(&mut self.buf[self.used..], s.as_bytes());
2022-07-06 04:13:09 +00:00
// Silent truncate
Ok(())
}
}
pub fn fmt_to_buf(buf: &mut [u8], args: Arguments) -> usize {
let mut w = BufFmtWriter::new(buf);
if let Ok(()) = fmt::write(&mut w, args) {
w.used
} else {
0
}
}
2022-08-09 05:53:37 +00:00
2022-09-15 08:17:05 +00:00
#[macro_export]
macro_rules! bfmt {
($buf:expr, $($args:tt)*) => {
$crate::fmt_to_buf($buf, format_args!($($args)*));
};
}
#[macro_export]
macro_rules! bfmt_cstr {
($buf:expr, $($args:tt)*) => {{
let len = $crate::fmt_to_buf($buf, format_args!($($args)*));
unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(&$buf[..(len + 1)]) }
}};
}
2023-05-25 08:03:04 +00:00
// The cstr! macro is copied from https://github.com/bytecodealliance/rustix/blob/main/src/cstr.rs
2022-08-09 05:53:37 +00:00
#[macro_export]
macro_rules! cstr {
2023-05-25 08:03:04 +00:00
($str:literal) => {{
assert!(
!$str.bytes().any(|b| b == b'\0'),
"cstr argument contains embedded NUL bytes",
);
unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($str, "\0").as_bytes()) }
2022-08-09 05:53:37 +00:00
}};
}
2023-05-02 23:49:43 +00:00
#[macro_export]
2023-05-25 08:03:04 +00:00
macro_rules! raw_cstr {
2023-05-02 23:49:43 +00:00
($s:literal) => {{
cstr!($s).as_ptr()
}};
}
2022-09-15 08:17:05 +00:00
pub fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str {
2023-01-19 19:45:16 +00:00
if ptr.is_null() {
"(null)"
} else {
unsafe { CStr::from_ptr(ptr.cast()) }.to_str().unwrap_or("")
}
2022-08-09 05:53:37 +00:00
}
pub fn errno() -> &'static mut i32 {
2022-08-15 18:53:51 +00:00
unsafe { &mut *libc::__errno() }
2022-08-09 05:53:37 +00:00
}
pub fn error_str() -> &'static str {
unsafe { ptr_to_str(libc::strerror(*errno())) }
}
// When len is 0, don't care whether buf is null or not
#[inline]
pub unsafe fn slice_from_ptr<'a, T>(buf: *const T, len: usize) -> &'a [T] {
if len == 0 {
&[]
} else {
slice::from_raw_parts(buf, len)
}
}
// When len is 0, don't care whether buf is null or not
#[inline]
pub unsafe fn slice_from_ptr_mut<'a, T>(buf: *mut T, len: usize) -> &'a mut [T] {
if len == 0 {
&mut []
} else {
slice::from_raw_parts_mut(buf, len)
}
}
2023-05-10 01:54:38 +00:00
pub trait FlatData {
fn as_raw_bytes(&self) -> &[u8]
where
Self: Sized,
{
unsafe {
let self_ptr = self as *const Self as *const u8;
slice::from_raw_parts(self_ptr, std::mem::size_of::<Self>())
}
}
fn as_raw_bytes_mut(&mut self) -> &mut [u8]
where
Self: Sized,
{
unsafe {
let self_ptr = self as *mut Self as *mut u8;
slice::from_raw_parts_mut(self_ptr, std::mem::size_of::<Self>())
}
}
}