Magisk/native/src/base/misc.rs

170 lines
3.9 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;
use std::fmt::{Arguments, Debug};
use std::str::Utf8Error;
use std::{fmt, slice};
2022-07-06 04:13:09 +00:00
use thiserror::Error;
2023-06-09 09:00:37 +00:00
pub fn copy_str<T: AsRef<[u8]>>(dest: &mut [u8], src: T) -> usize {
let src = src.as_ref();
2023-05-02 23:49:43 +00:00
let len = min(src.len(), dest.len() - 1);
dest[..len].copy_from_slice(&src[..len]);
dest[len] = b'\0';
len
}
2023-06-09 09:00:37 +00:00
pub fn copy_cstr(dest: &mut [u8], src: &CStr) -> usize {
let src = src.to_bytes_with_nul();
let len = min(src.len(), dest.len());
dest[..len].copy_from_slice(&src[..len]);
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-06-09 09:00:37 +00:00
self.used += copy_str(&mut self.buf[self.used..], s);
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)*));
2023-06-09 09:00:37 +00:00
#[allow(unused_unsafe)]
2022-09-15 08:17:05 +00:00
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",
);
2023-06-09 09:00:37 +00:00
#[allow(unused_unsafe)]
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()
}};
}
#[derive(Debug, Error)]
pub enum StrErr {
#[error(transparent)]
Invalid(#[from] Utf8Error),
#[error("argument is null")]
NullPointer,
}
pub fn ptr_to_str_result<'a, T>(ptr: *const T) -> Result<&'a str, StrErr> {
if ptr.is_null() {
Err(StrErr::NullPointer)
} else {
unsafe { CStr::from_ptr(ptr.cast()) }
.to_str()
2023-05-31 05:23:11 +00:00
.map_err(StrErr::from)
}
}
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>())
}
}
}