forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_input.fe
More file actions
73 lines (62 loc) · 1.83 KB
/
Copy pathmemory_input.fe
File metadata and controls
73 lines (62 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use core::abi::{AbiSize, ByteInput, Decode}
use core::Copy
use core::num::IntDowncast
use ingot::abi::Sol
use ingot::abi::sol::decode_input
use ingot::evm::ops::{mload, mstore}
/// A view over an in-memory byte buffer.
pub struct MemoryBytes {
pub base: u256,
pub len: u256,
}
impl Copy for MemoryBytes {}
impl MemoryBytes {
pub fn new(base: u256, len: u256) -> Self {
MemoryBytes { base, len }
}
/// Decode Solidity ABI data from this memory view.
pub fn decode<T>(self) -> T
where T: Decode<Sol> + AbiSize
{
decode_input(self)
}
}
impl ByteInput for MemoryBytes {
fn len(self) -> u256 {
self.len
}
#[arithmetic(unchecked)]
fn word_at(self, _ byte_offset: u256) -> u256 {
mload(self.base + byte_offset)
}
#[arithmetic(unchecked)]
fn byte_at(self, _ byte_offset: u256) -> u8 {
let word_offset = byte_offset - (byte_offset % 32)
let word = mload(self.base + word_offset)
let idx = byte_offset - word_offset
let shift = (31 - idx) * 8
(word >> shift).downcast_truncate()
}
fn copy_to_memory(self, dest: u256, src_offset: u256, len: u256) {
if len % 32 != 0 {
core::panic()
}
let end = src_offset + len
if end < src_offset || end > self.len {
core::panic()
}
let mut offset: u256 = 0
while offset < len {
mstore(addr: dest + offset, value: mload(self.base + src_offset + offset))
offset += 32
}
}
#[arithmetic(unchecked)]
fn copy_to_memory_unchecked_aligned(self, dest: u256, src_offset: u256, len: u256) {
let mut offset: u256 = 0
while offset < len {
mstore(addr: dest + offset, value: mload(self.base + src_offset + offset))
offset += 32
}
}
}