2022-07-06 04:13:09 +00:00
|
|
|
use std::cmp::min;
|
2023-06-12 08:07:43 +00:00
|
|
|
use std::ffi::{CStr, FromBytesWithNulError, OsStr};
|
|
|
|
use std::fmt::{Arguments, Debug, Display, Formatter};
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::path::Path;
|
2023-07-04 04:57:28 +00:00
|
|
|
use std::process::exit;
|
2023-05-26 06:45:38 +00:00
|
|
|
use std::str::Utf8Error;
|
2023-06-23 08:50:33 +00:00
|
|
|
use std::{fmt, io, mem, slice, str};
|
2022-07-06 04:13:09 +00:00
|
|
|
|
2023-07-04 04:57:28 +00:00
|
|
|
use argh::EarlyExit;
|
2023-06-12 08:07:43 +00:00
|
|
|
use libc::c_char;
|
2023-05-26 06:45:38 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2023-06-21 01:17:26 +00:00
|
|
|
use crate::ffi;
|
|
|
|
|
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-12 12:59:50 +00:00
|
|
|
pub fn copy_cstr<T: AsRef<CStr> + ?Sized>(dest: &mut [u8], src: &T) -> usize {
|
|
|
|
let src = src.as_ref().to_bytes_with_nul();
|
2023-06-09 09:00:37 +00:00
|
|
|
let len = min(src.len(), dest.len());
|
|
|
|
dest[..len].copy_from_slice(&src[..len]);
|
2023-06-15 11:00:32 +00:00
|
|
|
len - 1
|
2023-06-09 09:00:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 23:44:44 +00:00
|
|
|
pub struct BufFormatter<'a> {
|
2022-07-06 04:13:09 +00:00
|
|
|
buf: &'a mut [u8],
|
2023-06-29 23:44:44 +00:00
|
|
|
pub used: usize,
|
2022-07-06 04:13:09 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 23:44:44 +00:00
|
|
|
impl<'a> BufFormatter<'a> {
|
|
|
|
pub fn new(buf: &'a mut [u8]) -> Self {
|
|
|
|
BufFormatter { buf, used: 0 }
|
2022-07-06 04:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 23:44:44 +00:00
|
|
|
impl<'a> fmt::Write for BufFormatter<'a> {
|
2022-07-06 04:13:09 +00:00
|
|
|
// 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 {
|
2023-06-29 23:44:44 +00:00
|
|
|
let mut w = BufFormatter::new(buf);
|
2022-07-06 04:13:09 +00:00
|
|
|
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-15 11:00:32 +00:00
|
|
|
#[allow(unused_unsafe, clippy::unnecessary_mut_passed)]
|
2023-06-12 12:59:50 +00:00
|
|
|
unsafe {
|
|
|
|
$crate::Utf8CStr::from_bytes_unchecked($buf.get_unchecked(..(len + 1)))
|
|
|
|
}
|
2022-09-15 08:17:05 +00:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
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-06-23 12:37:06 +00:00
|
|
|
($($str:tt)*) => {{
|
2023-05-25 08:03:04 +00:00
|
|
|
assert!(
|
2023-06-23 12:37:06 +00:00
|
|
|
!($($str)*).bytes().any(|b| b == b'\0'),
|
2023-05-25 08:03:04 +00:00
|
|
|
"cstr argument contains embedded NUL bytes",
|
|
|
|
);
|
2023-06-09 09:00:37 +00:00
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
unsafe {
|
2023-06-23 12:37:06 +00:00
|
|
|
$crate::Utf8CStr::from_bytes_unchecked(concat!($($str)*, "\0").as_bytes())
|
2023-06-09 09:00:37 +00:00
|
|
|
}
|
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-09-07 03:45:59 +00:00
|
|
|
($($str:tt)*) => {{
|
|
|
|
cstr!($($str)*).as_ptr()
|
2023-05-02 23:49:43 +00:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2023-05-26 06:45:38 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum StrErr {
|
|
|
|
#[error(transparent)]
|
2023-06-12 08:07:43 +00:00
|
|
|
Utf8Error(#[from] Utf8Error),
|
|
|
|
#[error(transparent)]
|
|
|
|
CStrError(#[from] FromBytesWithNulError),
|
2023-05-26 06:45:38 +00:00
|
|
|
#[error("argument is null")]
|
2023-06-12 08:07:43 +00:00
|
|
|
NullPointerError,
|
|
|
|
}
|
|
|
|
|
2023-09-06 22:52:14 +00:00
|
|
|
pub trait StringExt {
|
|
|
|
fn nul_terminate(&mut self) -> &mut [u8];
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StringExt for String {
|
|
|
|
fn nul_terminate(&mut self) -> &mut [u8] {
|
|
|
|
self.reserve(1);
|
|
|
|
// SAFETY: the string is reserved to have enough capacity to fit in the null byte
|
|
|
|
// SAFETY: the null byte is explicitly added outside of the string's length
|
|
|
|
unsafe {
|
|
|
|
let buf = slice::from_raw_parts_mut(self.as_mut_ptr(), self.len() + 1);
|
|
|
|
*buf.get_unchecked_mut(self.len()) = b'\0';
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 08:07:43 +00:00
|
|
|
// The better CStr: UTF-8 validated + null terminated buffer
|
2023-09-06 22:52:14 +00:00
|
|
|
#[derive(PartialEq)]
|
2023-06-12 08:07:43 +00:00
|
|
|
pub struct Utf8CStr {
|
|
|
|
inner: [u8],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Utf8CStr {
|
|
|
|
pub fn from_cstr(cstr: &CStr) -> Result<&Utf8CStr, StrErr> {
|
|
|
|
// Validate the buffer during construction
|
|
|
|
str::from_utf8(cstr.to_bytes())?;
|
|
|
|
Ok(unsafe { Self::from_bytes_unchecked(cstr.to_bytes_with_nul()) })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_bytes(buf: &[u8]) -> Result<&Utf8CStr, StrErr> {
|
|
|
|
Self::from_cstr(CStr::from_bytes_with_nul(buf)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_string(s: &mut String) -> &Utf8CStr {
|
2023-09-06 22:52:14 +00:00
|
|
|
let buf = s.nul_terminate();
|
|
|
|
// SAFETY: the null byte is explicitly added to the buffer
|
|
|
|
unsafe { Self::from_bytes_unchecked(buf) }
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 12:59:50 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
pub unsafe fn from_bytes_unchecked(buf: &[u8]) -> &Utf8CStr {
|
2023-06-23 08:50:33 +00:00
|
|
|
mem::transmute(buf)
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> Result<&'a Utf8CStr, StrErr> {
|
|
|
|
if ptr.is_null() {
|
|
|
|
return Err(StrErr::NullPointerError);
|
|
|
|
}
|
|
|
|
Self::from_cstr(unsafe { CStr::from_ptr(ptr) })
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:59:50 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
// The length of the slice is at least 1 due to null termination check
|
|
|
|
unsafe { self.inner.get_unchecked(..self.inner.len() - 1) }
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:59:50 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
pub fn as_bytes_with_nul(&self) -> &[u8] {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:59:50 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
pub fn as_ptr(&self) -> *const c_char {
|
|
|
|
self.inner.as_ptr().cast()
|
|
|
|
}
|
2023-06-12 12:59:50 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn as_cstr(&self) -> &CStr {
|
2023-06-23 08:50:33 +00:00
|
|
|
// SAFETY: Already validated as null terminated during construction
|
|
|
|
unsafe { CStr::from_bytes_with_nul_unchecked(&self.inner) }
|
2023-06-12 12:59:50 +00:00
|
|
|
}
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Utf8CStr {
|
|
|
|
type Target = str;
|
|
|
|
|
2023-06-12 12:59:50 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
fn deref(&self) -> &str {
|
2023-06-23 08:50:33 +00:00
|
|
|
// SAFETY: Already UTF-8 validated during construction
|
|
|
|
unsafe { str::from_utf8_unchecked(self.as_bytes()) }
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Utf8CStr {
|
2023-06-12 12:59:50 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
Display::fmt(self.deref(), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Utf8CStr {
|
2023-06-12 12:59:50 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
Debug::fmt(self.deref(), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<CStr> for Utf8CStr {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &CStr {
|
2023-06-23 08:50:33 +00:00
|
|
|
self.as_cstr()
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for Utf8CStr {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &str {
|
2023-06-23 08:50:33 +00:00
|
|
|
self.deref()
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<OsStr> for Utf8CStr {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &OsStr {
|
2023-06-12 12:59:50 +00:00
|
|
|
OsStr::new(self.deref())
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Path> for Utf8CStr {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &Path {
|
2023-06-12 12:59:50 +00:00
|
|
|
Path::new(self.deref())
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
2023-05-26 06:45:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 12:59:50 +00:00
|
|
|
impl PartialEq<CStr> for Utf8CStr {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &CStr) -> bool {
|
|
|
|
self.as_cstr() == other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<str> for Utf8CStr {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &str) -> bool {
|
|
|
|
self.deref() == other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<Utf8CStr> for CStr {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Utf8CStr) -> bool {
|
|
|
|
self == other.as_cstr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<Utf8CStr> for str {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Utf8CStr) -> bool {
|
|
|
|
self == other.deref()
|
2023-05-26 06:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-15 23:56:22 +00:00
|
|
|
// 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
|
|
|
|
2023-06-14 14:24:42 +00:00
|
|
|
pub trait FlatData
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
fn as_raw_bytes(&self) -> &[u8] {
|
2023-05-10 01:54:38 +00:00
|
|
|
unsafe {
|
|
|
|
let self_ptr = self as *const Self as *const u8;
|
2023-07-04 04:57:28 +00:00
|
|
|
slice::from_raw_parts(self_ptr, mem::size_of::<Self>())
|
2023-05-10 01:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 14:24:42 +00:00
|
|
|
fn as_raw_bytes_mut(&mut self) -> &mut [u8] {
|
2023-05-10 01:54:38 +00:00
|
|
|
unsafe {
|
|
|
|
let self_ptr = self as *mut Self as *mut u8;
|
2023-07-04 04:57:28 +00:00
|
|
|
slice::from_raw_parts_mut(self_ptr, mem::size_of::<Self>())
|
2023-05-10 01:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 14:24:42 +00:00
|
|
|
|
|
|
|
fn bytes_size(&self) -> usize {
|
2023-07-04 04:57:28 +00:00
|
|
|
mem::size_of::<Self>()
|
2023-06-14 14:24:42 +00:00
|
|
|
}
|
2023-05-10 01:54:38 +00:00
|
|
|
}
|
2023-06-12 08:07:43 +00:00
|
|
|
|
2023-06-23 08:50:33 +00:00
|
|
|
macro_rules! impl_flat_data {
|
|
|
|
($($t:ty)*) => ($(impl FlatData for $t {})*)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_flat_data!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
|
2023-06-14 14:24:42 +00:00
|
|
|
|
2023-06-23 08:50:33 +00:00
|
|
|
// Check libc return value and map to Result
|
|
|
|
pub trait LibcReturn
|
|
|
|
where
|
|
|
|
Self: Copy,
|
|
|
|
{
|
2023-06-12 08:07:43 +00:00
|
|
|
fn is_error(&self) -> bool;
|
|
|
|
fn check_os_err(self) -> io::Result<Self> {
|
|
|
|
if self.is_error() {
|
2023-06-23 08:50:33 +00:00
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
} else {
|
|
|
|
Ok(self)
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 08:50:33 +00:00
|
|
|
macro_rules! impl_libc_return {
|
|
|
|
($($t:ty)*) => ($(
|
|
|
|
impl LibcReturn for $t {
|
|
|
|
#[inline]
|
|
|
|
fn is_error(&self) -> bool {
|
|
|
|
*self < 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*)
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 08:50:33 +00:00
|
|
|
impl_libc_return! { i8 i16 i32 i64 isize }
|
2023-06-12 08:07:43 +00:00
|
|
|
|
|
|
|
impl<T> LibcReturn for *const T {
|
2023-06-23 08:50:33 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
fn is_error(&self) -> bool {
|
|
|
|
self.is_null()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> LibcReturn for *mut T {
|
2023-06-23 08:50:33 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
fn is_error(&self) -> bool {
|
|
|
|
self.is_null()
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 01:17:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 04:57:28 +00:00
|
|
|
|
2023-07-06 17:06:14 +00:00
|
|
|
// SAFETY: libc guarantees argc and argv are properly setup and are static
|
2023-07-04 04:57:28 +00:00
|
|
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
2023-07-06 17:06:14 +00:00
|
|
|
pub fn map_args(argc: i32, argv: *const *const c_char) -> Result<Vec<&'static str>, StrErr> {
|
2023-07-04 04:57:28 +00:00
|
|
|
unsafe { slice::from_raw_parts(argv, argc as usize) }
|
|
|
|
.iter()
|
2023-07-06 17:06:14 +00:00
|
|
|
.map(|s| unsafe { Utf8CStr::from_ptr(*s) }.map(|s| s.deref()))
|
2023-07-04 04:57:28 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait EarlyExitExt<T> {
|
2023-07-06 00:05:39 +00:00
|
|
|
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T;
|
2023-07-04 04:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> EarlyExitExt<T> for Result<T, EarlyExit> {
|
2023-07-06 00:05:39 +00:00
|
|
|
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T {
|
2023-07-04 04:57:28 +00:00
|
|
|
match self {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(EarlyExit { output, status }) => match status {
|
|
|
|
Ok(_) => {
|
2023-07-06 00:05:39 +00:00
|
|
|
print_help_msg();
|
2023-07-04 04:57:28 +00:00
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!("{}", output);
|
2023-07-06 00:05:39 +00:00
|
|
|
print_help_msg();
|
2023-07-04 04:57:28 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|