mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-01-03 03:07:39 +00:00
Update cstr macro
This commit is contained in:
parent
18d0cedbe2
commit
533aeadd38
@ -58,59 +58,21 @@ macro_rules! bfmt_cstr {
|
||||
}};
|
||||
}
|
||||
|
||||
// The cstr! macro is inspired by https://github.com/Nugine/const-str
|
||||
|
||||
macro_rules! const_assert {
|
||||
($s: expr) => {
|
||||
assert!($s)
|
||||
};
|
||||
}
|
||||
|
||||
pub struct ToCStr<'a>(pub &'a str);
|
||||
|
||||
impl ToCStr<'_> {
|
||||
const fn assert_no_nul(&self) {
|
||||
let bytes = self.0.as_bytes();
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
const_assert!(bytes[i] != 0);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn eval_len(&self) -> usize {
|
||||
self.assert_no_nul();
|
||||
self.0.as_bytes().len() + 1
|
||||
}
|
||||
|
||||
pub const fn eval_bytes<const N: usize>(&self) -> [u8; N] {
|
||||
let mut buf = [0; N];
|
||||
let mut pos = 0;
|
||||
let bytes = self.0.as_bytes();
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
const_assert!(bytes[i] != 0);
|
||||
buf[pos] = bytes[i];
|
||||
pos += 1;
|
||||
i += 1;
|
||||
}
|
||||
pos += 1;
|
||||
const_assert!(pos == N);
|
||||
buf
|
||||
}
|
||||
}
|
||||
// The cstr! macro is copied from https://github.com/bytecodealliance/rustix/blob/main/src/cstr.rs
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! cstr {
|
||||
($s:literal) => {{
|
||||
const LEN: usize = $crate::ToCStr($s).eval_len();
|
||||
const BUF: [u8; LEN] = $crate::ToCStr($s).eval_bytes();
|
||||
unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(&BUF) }
|
||||
($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()) }
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! str_ptr {
|
||||
macro_rules! raw_cstr {
|
||||
($s:literal) => {{
|
||||
cstr!($s).as_ptr()
|
||||
}};
|
||||
|
@ -62,12 +62,12 @@ pub fn android_logging() {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
fmt_to_buf(&mut buf, args);
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), buf.as_ptr());
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
|
||||
}
|
||||
}
|
||||
fn android_log_write(level: LogLevel, msg: &[u8]) {
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), msg.as_ptr());
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,13 +87,13 @@ pub fn magisk_logging() {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
let len = fmt_to_buf(&mut buf, args);
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), buf.as_ptr());
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
|
||||
}
|
||||
magisk_log_write(level_to_prio(level), &buf[..len]);
|
||||
}
|
||||
fn magisk_write(level: LogLevel, msg: &[u8]) {
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), msg.as_ptr());
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
|
||||
}
|
||||
magisk_log_write(level_to_prio(level), &msg);
|
||||
}
|
||||
@ -114,13 +114,13 @@ pub fn zygisk_logging() {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
let len = fmt_to_buf(&mut buf, args);
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), buf.as_ptr());
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
|
||||
}
|
||||
zygisk_log_write(level_to_prio(level), &buf[..len]);
|
||||
}
|
||||
fn zygisk_write(level: LogLevel, msg: &[u8]) {
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), msg.as_ptr());
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
|
||||
}
|
||||
zygisk_log_write(level_to_prio(level), &msg);
|
||||
}
|
||||
@ -312,7 +312,7 @@ extern "C" fn logfile_writer(arg: *mut c_void) -> *mut c_void {
|
||||
aux_len = strftime(
|
||||
aux.as_mut_ptr().cast(),
|
||||
aux.len(),
|
||||
str_ptr!("%m-%d %T"),
|
||||
raw_cstr!("%m-%d %T"),
|
||||
&tm,
|
||||
);
|
||||
let ms = ts.tv_nsec / 1000000;
|
||||
|
@ -16,10 +16,10 @@ static KMSG: OnceLock<File> = OnceLock::new();
|
||||
pub fn setup_klog() {
|
||||
// Shut down first 3 fds
|
||||
unsafe {
|
||||
let mut fd = open(str_ptr!("/dev/null"), O_RDWR | O_CLOEXEC);
|
||||
let mut fd = open(raw_cstr!("/dev/null"), O_RDWR | O_CLOEXEC);
|
||||
if fd < 0 {
|
||||
mknod(str_ptr!("/null"), S_IFCHR | 0666, makedev(1, 3));
|
||||
fd = open(str_ptr!("/null"), O_RDWR | O_CLOEXEC);
|
||||
mknod(raw_cstr!("/null"), S_IFCHR | 0666, makedev(1, 3));
|
||||
fd = open(raw_cstr!("/null"), O_RDWR | O_CLOEXEC);
|
||||
fs::remove_file("/null").ok();
|
||||
}
|
||||
|
||||
@ -35,10 +35,10 @@ pub fn setup_klog() {
|
||||
KMSG.set(kmsg).ok();
|
||||
} else {
|
||||
unsafe {
|
||||
mknod(str_ptr!("/kmsg"), S_IFCHR | 0666, makedev(1, 11));
|
||||
mknod(raw_cstr!("/kmsg"), S_IFCHR | 0666, makedev(1, 11));
|
||||
KMSG.set(File::options().write(true).open("/kmsg").unwrap())
|
||||
.ok();
|
||||
unlink(str_ptr!("/kmsg"));
|
||||
unlink(raw_cstr!("/kmsg"));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user