mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-06-26 09:52:34 +00:00
refactor(core): move c string conversion out of UI code
[no changelog]
This commit is contained in:
parent
4e31a9e20a
commit
ccb28f4aab
@ -44,6 +44,9 @@ mod trezorhal;
|
|||||||
#[cfg(feature = "ui")]
|
#[cfg(feature = "ui")]
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
|
|
||||||
|
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
|
@ -7,9 +7,9 @@ use crate::{
|
|||||||
},
|
},
|
||||||
ui::{
|
ui::{
|
||||||
ui_bootloader::{BootloaderLayoutType as _, BootloaderUI},
|
ui_bootloader::{BootloaderLayoutType as _, BootloaderUI},
|
||||||
util::{from_c_array, from_c_str},
|
|
||||||
ModelUI,
|
ModelUI,
|
||||||
},
|
},
|
||||||
|
util::{from_c_array, from_c_str},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -3,9 +3,7 @@
|
|||||||
|
|
||||||
use crate::ui::{CommonUI, ModelUI};
|
use crate::ui::{CommonUI, ModelUI};
|
||||||
|
|
||||||
use crate::ui::shape;
|
use crate::{ui::shape, util::from_c_str};
|
||||||
|
|
||||||
use crate::ui::util::from_c_str;
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn display_rsod_rust(
|
extern "C" fn display_rsod_rust(
|
||||||
|
@ -5,9 +5,9 @@ use crate::{
|
|||||||
},
|
},
|
||||||
ui::{
|
ui::{
|
||||||
ui_prodtest::{ProdtestLayoutType, ProdtestUI},
|
ui_prodtest::{ProdtestLayoutType, ProdtestUI},
|
||||||
util::from_c_array,
|
|
||||||
ModelUI,
|
ModelUI,
|
||||||
},
|
},
|
||||||
|
util::from_c_array,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "touch")]
|
#[cfg(feature = "touch")]
|
||||||
|
@ -15,50 +15,6 @@ impl<T, E> ResultExt for Result<T, E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Constructs a string from a C string.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// The caller is responsible that the pointer is valid, which means that:
|
|
||||||
/// (a) it points to a memory containing a valid C string (zero-terminated
|
|
||||||
/// sequence of characters), and
|
|
||||||
/// (b) that the pointer has appropriate lifetime.
|
|
||||||
pub unsafe fn from_c_str<'a>(c_str: *const cty::c_char) -> Option<&'a str> {
|
|
||||||
if c_str.is_null() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
unsafe {
|
|
||||||
let bytes = core::ffi::CStr::from_ptr(c_str as _).to_bytes();
|
|
||||||
if bytes.is_ascii() {
|
|
||||||
Some(core::str::from_utf8_unchecked(bytes))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct str from a C array.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// The caller is responsible that the pointer is valid, which means that:
|
|
||||||
/// (a) it points to a memory containing array of characters, with length `len`,
|
|
||||||
/// and
|
|
||||||
/// (b) that the pointer has appropriate lifetime.
|
|
||||||
pub unsafe fn from_c_array<'a>(c_str: *const cty::c_char, len: usize) -> Option<&'a str> {
|
|
||||||
if c_str.is_null() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
unsafe {
|
|
||||||
let slice = core::slice::from_raw_parts(c_str as *const u8, len);
|
|
||||||
if slice.is_ascii() {
|
|
||||||
Some(core::str::from_utf8_unchecked(slice))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "ui_debug")]
|
#[cfg(feature = "ui_debug")]
|
||||||
static mut DISABLE_ANIMATION: bool = false;
|
static mut DISABLE_ANIMATION: bool = false;
|
||||||
|
|
||||||
|
43
core/embed/rust/src/util/mod.rs
Normal file
43
core/embed/rust/src/util/mod.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/// Constructs a string from a C string.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The caller is responsible that the pointer is valid, which means that:
|
||||||
|
/// (a) it points to a memory containing a valid C string (zero-terminated
|
||||||
|
/// sequence of characters), and
|
||||||
|
/// (b) that the pointer has appropriate lifetime.
|
||||||
|
pub unsafe fn from_c_str<'a>(c_str: *const cty::c_char) -> Option<&'a str> {
|
||||||
|
if c_str.is_null() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
let bytes = core::ffi::CStr::from_ptr(c_str as _).to_bytes();
|
||||||
|
if bytes.is_ascii() {
|
||||||
|
Some(core::str::from_utf8_unchecked(bytes))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Construct str from a C array.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The caller is responsible that the pointer is valid, which means that:
|
||||||
|
/// (a) it points to a memory containing array of characters, with length `len`,
|
||||||
|
/// and
|
||||||
|
/// (b) that the pointer has appropriate lifetime.
|
||||||
|
pub unsafe fn from_c_array<'a>(c_str: *const cty::c_char, len: usize) -> Option<&'a str> {
|
||||||
|
if c_str.is_null() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
let slice = core::slice::from_raw_parts(c_str as *const u8, len);
|
||||||
|
if slice.is_ascii() {
|
||||||
|
Some(core::str::from_utf8_unchecked(slice))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user