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
use alloc::vec::Vec;
use crate::Opcode;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Valids(Vec<bool>);
impl Valids {
pub fn new(code: &[u8]) -> Self {
let mut valids: Vec<bool> = Vec::with_capacity(code.len());
valids.resize(code.len(), false);
let mut i = 0;
while i < code.len() {
let opcode = Opcode(code[i]);
if opcode == Opcode::JUMPDEST {
valids[i] = true;
i += 1;
} else if let Some(v) = opcode.is_push() {
i += v as usize + 1;
} else {
i += 1;
}
}
Valids(valids)
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn is_valid(&self, position: usize) -> bool {
if position >= self.0.len() {
return false;
}
if !self.0[position] {
return false;
}
true
}
}