1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 13:38:12 +00:00

fix(core/rust): make Toif constructor return a Result instead of Option

which is slightly more correct
This commit is contained in:
matejcik 2023-11-27 12:50:46 +01:00 committed by matejcik
parent d674634c86
commit ba6cce2bbc
2 changed files with 8 additions and 7 deletions

View File

@ -1,4 +1,5 @@
use crate::{
error::Error,
trezorhal::{
display::ToifFormat,
uzlib::{UzlibContext, UZLIB_WINDOW_SIZE},
@ -210,16 +211,16 @@ pub struct Toif<'i> {
}
impl<'i> Toif<'i> {
pub const fn new(data: &'i [u8]) -> Option<Self> {
pub const fn new(data: &'i [u8]) -> Result<Self, Error> {
if data.len() < TOIF_HEADER_LENGTH || data[0] != b'T' || data[1] != b'O' || data[2] != b'I'
{
return None;
return Err(value_error!("Invalid TOIF header."));
}
let zdatalen = u32::from_le_bytes([data[8], data[9], data[10], data[11]]) as usize;
if zdatalen + TOIF_HEADER_LENGTH != data.len() {
return None;
return Err(value_error!("Invalid TOIF length."));
}
Some(Self {
Ok(Self {
data,
empty_right_column: false,
})
@ -317,8 +318,8 @@ pub struct Icon {
impl Icon {
pub const fn new(data: &'static [u8]) -> Self {
let toif = match Toif::new(data) {
Some(t) => t,
None => panic!("Invalid image."),
Ok(t) => t,
_ => panic!("Invalid image."),
};
assert!(matches!(toif.format(), ToifFormat::GrayScaleEH));
Self {

View File

@ -380,7 +380,7 @@ fn is_image_jpeg(buffer: &[u8]) -> bool {
fn is_image_toif(buffer: &[u8]) -> bool {
let toif = Toif::new(buffer);
if let Some(toif) = toif {
if let Ok(toif) = toif {
if toif.size().x == HOMESCREEN_TOIF_SIZE
&& toif.size().y == HOMESCREEN_TOIF_SIZE
&& toif.format() == ToifFormat::FullColorBE