#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use bigint::{U256, M256};
use super::errors::NotSupportedError;
use super::Patch;
#[cfg(feature = "std")] use std::marker::PhantomData;
#[cfg(not(feature = "std"))] use core::marker::PhantomData;
pub trait Memory {
fn check_write(&self, index: U256) -> Result<(), NotSupportedError>;
fn check_write_range(&self, start: U256, len: U256) -> Result<(), NotSupportedError>;
fn write(&mut self, index: U256, value: M256) -> Result<(), NotSupportedError>;
fn write_raw(&mut self, index: U256, value: u8) -> Result<(), NotSupportedError>;
fn read(&self, index: U256) -> M256;
fn read_raw(&self, index: U256) -> u8;
}
pub struct SeqMemory<P: Patch> {
memory: Vec<u8>,
_marker: PhantomData<P>,
}
impl<P: Patch> Default for SeqMemory<P> {
fn default() -> SeqMemory<P> {
SeqMemory {
memory: Vec::new(),
_marker: PhantomData,
}
}
}
impl<P: Patch> SeqMemory<P> {
#[inline]
pub fn len(&self) -> usize {
self.memory.len()
}
#[inline]
pub fn is_empty(&self) -> bool { self.len() == 0 }
}
impl<P: Patch> Memory for SeqMemory<P> {
fn check_write(&self, index: U256) -> Result<(), NotSupportedError> {
let end = index + 32.into();
if end > U256::from(P::memory_limit()) {
Err(NotSupportedError::MemoryIndexNotSupported)
} else {
Ok(())
}
}
fn check_write_range(&self, start: U256, len: U256) -> Result<(), NotSupportedError> {
if len == U256::zero() {
return Ok(());
}
if start.saturating_add(len) > U256::from(P::memory_limit()) {
Err(NotSupportedError::MemoryIndexNotSupported)
} else {
self.check_write(start + len - U256::from(1u64))
}
}
fn write(&mut self, index: U256, value: M256) -> Result<(), NotSupportedError> {
let end = M256::from(index) + 32.into();
if end > M256::from(P::memory_limit()) {
return Err(NotSupportedError::MemoryIndexNotSupported);
}
for i in 0..32 {
self.write_raw(index + i.into(), value.index(i)).unwrap();
}
Ok(())
}
fn write_raw(&mut self, index: U256, value: u8) -> Result<(), NotSupportedError> {
if index > U256::from(P::memory_limit()) {
return Err(NotSupportedError::MemoryIndexNotSupported);
}
let index: usize = index.as_usize();
if self.memory.len() <= index {
self.memory.resize(index + 1, 0u8);
}
self.memory[index] = value;
Ok(())
}
fn read(&self, index: U256) -> M256 {
let mut a: [u8; 32] = [0u8; 32];
for (i, item) in a[0..32].iter_mut().enumerate() {
*item = self.read_raw(index + i.into());
}
a.as_ref().into()
}
fn read_raw(&self, index: U256) -> u8 {
if index > U256::from(usize::max_value()) {
return 0u8;
}
let index: usize = index.as_usize();
if self.memory.len() <= index {
return 0u8;
}
self.memory[index]
}
}