Support extracting any partition from payload.bin

This commit is contained in:
topjohnwu
2023-05-25 23:45:38 -07:00
committed by John Wu
parent ec31cab5a7
commit 659b9c6fee
14 changed files with 203 additions and 116 deletions

View File

@@ -1,8 +1,11 @@
use std::cmp::min;
use std::ffi::CStr;
use std::fmt::Arguments;
use std::fmt::{Arguments, Debug};
use std::str::Utf8Error;
use std::{fmt, slice};
use thiserror::Error;
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]);
@@ -78,6 +81,24 @@ macro_rules! raw_cstr {
}};
}
#[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()
.map_err(|e| StrErr::from(e))
}
}
pub fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str {
if ptr.is_null() {
"(null)"