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

pull/3491/head
matejcik 5 months ago
parent 751b876397
commit 1c507c2046

@ -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")

@ -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),
}
}
}

@ -0,0 +1,72 @@
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 read_stream(&mut self, len: usize) -> Result<Self, Error> {
let buf = self
.buf
.get(self.pos..self.pos + len)
.ok_or(Error::EOFError)?;
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(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)
.copied()
.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)
}
}

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

@ -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)
}
}

Loading…
Cancel
Save