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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::{
BinaryReader, BinaryReaderError, InitExpr, Range, Result, SectionIteratorLimited,
SectionReader, SectionWithLimitedItems,
};
#[derive(Debug, Copy, Clone)]
pub struct Data<'a> {
pub kind: DataKind<'a>,
pub data: &'a [u8],
}
#[derive(Debug, Copy, Clone)]
pub enum DataKind<'a> {
Passive,
Active {
memory_index: u32,
init_expr: InitExpr<'a>,
},
}
#[derive(Clone)]
pub struct DataSectionReader<'a> {
reader: BinaryReader<'a>,
count: u32,
forbid_bulk_memory: bool,
}
impl<'a> DataSectionReader<'a> {
pub fn new(data: &'a [u8], offset: usize) -> Result<DataSectionReader<'a>> {
let mut reader = BinaryReader::new_with_offset(data, offset);
let count = reader.read_var_u32()?;
Ok(DataSectionReader {
reader,
count,
forbid_bulk_memory: false,
})
}
pub fn original_position(&self) -> usize {
self.reader.original_position()
}
pub fn get_count(&self) -> u32 {
self.count
}
pub fn forbid_bulk_memory(&mut self, forbid: bool) {
self.forbid_bulk_memory = forbid;
}
fn verify_data_end(&self, end: usize) -> Result<()> {
if self.reader.buffer.len() < end {
return Err(BinaryReaderError::new(
"Data segment extends past end of the data section",
self.reader.original_offset + self.reader.buffer.len(),
));
}
Ok(())
}
pub fn read<'b>(&mut self) -> Result<Data<'b>>
where
'a: 'b,
{
let flags = self.reader.read_var_u32()?;
let kind = if !self.forbid_bulk_memory && flags == 1 {
DataKind::Passive
} else {
let memory_index = match flags {
0 => 0,
_ if self.forbid_bulk_memory => flags,
2 => self.reader.read_var_u32()?,
_ => {
return Err(BinaryReaderError::new(
"invalid flags byte in data segment",
self.reader.original_position() - 1,
));
}
};
let init_expr = {
let expr_offset = self.reader.position;
self.reader.skip_init_expr()?;
let data = &self.reader.buffer[expr_offset..self.reader.position];
InitExpr::new(data, self.reader.original_offset + expr_offset)
};
DataKind::Active {
memory_index,
init_expr,
}
};
let data_len = self.reader.read_var_u32()? as usize;
let data_end = self.reader.position + data_len;
self.verify_data_end(data_end)?;
let data = &self.reader.buffer[self.reader.position..data_end];
self.reader.skip_to(data_end);
Ok(Data { kind, data })
}
}
impl<'a> SectionReader for DataSectionReader<'a> {
type Item = Data<'a>;
fn read(&mut self) -> Result<Self::Item> {
DataSectionReader::read(self)
}
fn eof(&self) -> bool {
self.reader.eof()
}
fn original_position(&self) -> usize {
DataSectionReader::original_position(self)
}
fn range(&self) -> Range {
self.reader.range()
}
}
impl<'a> SectionWithLimitedItems for DataSectionReader<'a> {
fn get_count(&self) -> u32 {
DataSectionReader::get_count(self)
}
}
impl<'a> IntoIterator for DataSectionReader<'a> {
type Item = Result<Data<'a>>;
type IntoIter = SectionIteratorLimited<DataSectionReader<'a>>;
fn into_iter(self) -> Self::IntoIter {
SectionIteratorLimited::new(self)
}
}