1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00

refactor(core/rust): move InputStream to its own module

This commit is contained in:
matejcik 2024-01-15 10:44:25 +01:00 committed by Jiří Musil
parent 519e591d91
commit 76296ad417
5 changed files with 77 additions and 53 deletions

View File

@ -219,6 +219,7 @@ fn generate_micropython_bindings() {
.allowlist_function("mp_obj_new_exception_args")
.allowlist_function("trezor_obj_call_protected")
.allowlist_var("mp_type_AttributeError")
.allowlist_var("mp_type_EOFError")
.allowlist_var("mp_type_IndexError")
.allowlist_var("mp_type_KeyError")
.allowlist_var("mp_type_MemoryError")

View File

@ -14,6 +14,7 @@ pub enum Error {
OutOfRange,
MissingKwargs,
AllocationFailed,
EOFError,
IndexError,
#[cfg(feature = "micropython")]
CaughtException(Obj),
@ -71,6 +72,7 @@ impl Error {
Error::AttributeError(attr) => {
ffi::mp_obj_new_exception_args(&ffi::mp_type_AttributeError, 1, &attr.into())
}
Error::EOFError => ffi::mp_obj_new_exception(&ffi::mp_type_EOFError),
}
}
}

71
core/embed/rust/src/io.rs Normal file
View File

@ -0,0 +1,71 @@
use crate::error::Error;
pub struct InputStream<'a> {
buf: &'a [u8],
pos: usize,
}
impl<'a> InputStream<'a> {
pub fn new(buf: &'a [u8]) -> Self {
Self { buf, pos: 0 }
}
pub fn remaining(&self) -> usize {
self.buf.len().saturating_sub(self.pos)
}
pub fn tell(&self) -> usize {
self.pos
}
pub fn read_stream(&mut self, len: usize) -> Result<Self, Error> {
self.read(len).map(Self::new)
}
pub fn read(&mut self, len: usize) -> Result<&'a [u8], Error> {
let buf = self
.buf
.get(self.pos..self.pos + len)
.ok_or(Error::EOFError)?;
self.pos += len;
Ok(buf)
}
pub fn rest(self) -> &'a [u8] {
if self.pos > self.buf.len() {
&[]
} else {
&self.buf[self.pos..]
}
}
pub fn read_byte(&mut self) -> Result<u8, Error> {
let val = self.buf.get(self.pos).ok_or(Error::EOFError)?;
self.pos += 1;
Ok(*val)
}
pub fn read_u16_le(&mut self) -> Result<u16, Error> {
let buf = self.read(2)?;
Ok(u16::from_le_bytes(unwrap!(buf.try_into())))
}
pub fn read_u32_le(&mut self) -> Result<u32, Error> {
let buf = self.read(4)?;
Ok(u32::from_le_bytes(unwrap!(buf.try_into())))
}
pub fn read_uvarint(&mut self) -> Result<u64, Error> {
let mut uint = 0;
let mut shift = 0;
loop {
let byte = self.read_byte()?;
uint += (byte as u64 & 0x7F) << shift;
shift += 7;
if byte & 0x80 == 0 {
break;
}
}
Ok(uint)
}
}

View File

@ -17,6 +17,8 @@ mod error;
// use trezorhal for its macros early
#[macro_use]
mod trezorhal;
mod io;
mod maybe_trace;
#[cfg(feature = "micropython")]
#[macro_use]

View File

@ -5,6 +5,7 @@ use core::{
use crate::{
error::Error,
io::InputStream,
micropython::{buffer, gc::Gc, list::List, map::Map, obj::Obj, qstr::Qstr, util},
};
@ -250,56 +251,3 @@ impl Decoder {
}
}
}
pub struct InputStream<'a> {
buf: &'a [u8],
pos: usize,
}
impl<'a> InputStream<'a> {
pub fn new(buf: &'a [u8]) -> Self {
Self { buf, pos: 0 }
}
pub fn read_stream(&mut self, len: usize) -> Result<Self, Error> {
let buf = self
.buf
.get(self.pos..self.pos + len)
.ok_or_else(error::end_of_buffer)?;
self.pos += len;
Ok(Self::new(buf))
}
pub fn read(&mut self, len: usize) -> Result<&[u8], Error> {
let buf = self
.buf
.get(self.pos..self.pos + len)
.ok_or_else(error::end_of_buffer)?;
self.pos += len;
Ok(buf)
}
pub fn read_byte(&mut self) -> Result<u8, Error> {
let val = self
.buf
.get(self.pos)
.copied()
.ok_or_else(error::end_of_buffer)?;
self.pos += 1;
Ok(val)
}
pub fn read_uvarint(&mut self) -> Result<u64, Error> {
let mut uint = 0;
let mut shift = 0;
loop {
let byte = self.read_byte()?;
uint += (byte as u64 & 0x7F) << shift;
shift += 7;
if byte & 0x80 == 0 {
break;
}
}
Ok(uint)
}
}