1 /* Copyright 2018 Mozilla Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 use super::{
17     BinaryReader, MemoryType, Result, SectionIteratorLimited, SectionReader,
18     SectionWithLimitedItems,
19 };
20 
21 pub struct MemorySectionReader<'a> {
22     reader: BinaryReader<'a>,
23     count: u32,
24 }
25 
26 impl<'a> MemorySectionReader<'a> {
new(data: &'a [u8], offset: usize) -> Result<MemorySectionReader<'a>>27     pub fn new(data: &'a [u8], offset: usize) -> Result<MemorySectionReader<'a>> {
28         let mut reader = BinaryReader::new_with_offset(data, offset);
29         let count = reader.read_var_u32()?;
30         Ok(MemorySectionReader { reader, count })
31     }
32 
original_position(&self) -> usize33     pub fn original_position(&self) -> usize {
34         self.reader.original_position()
35     }
36 
get_count(&self) -> u3237     pub fn get_count(&self) -> u32 {
38         self.count
39     }
40 
41     /// Reads content of the memory section.
42     ///
43     /// # Examples
44     /// ```
45     /// # let data: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
46     /// #     0x01, 0x4, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
47     /// #     0x05, 0x03, 0x01, 0x00, 0x02,
48     /// #     0x0a, 0x05, 0x01, 0x03, 0x00, 0x01, 0x0b];
49     /// use wasmparser::ModuleReader;
50     /// let mut reader = ModuleReader::new(data).expect("module reader");
51     /// let section = reader.read().expect("type section");
52     /// let section = reader.read().expect("function section");
53     /// let section = reader.read().expect("memory section");
54     /// let mut memory_reader = section.get_memory_section_reader().expect("memory section reader");
55     /// for _ in 0..memory_reader.get_count() {
56     ///     let memory = memory_reader.read().expect("memory");
57     ///     println!("Memory: {:?}", memory);
58     /// }
59     /// ```
read(&mut self) -> Result<MemoryType>60     pub fn read(&mut self) -> Result<MemoryType> {
61         self.reader.read_memory_type()
62     }
63 }
64 
65 impl<'a> SectionReader for MemorySectionReader<'a> {
66     type Item = MemoryType;
read(&mut self) -> Result<Self::Item>67     fn read(&mut self) -> Result<Self::Item> {
68         MemorySectionReader::read(self)
69     }
eof(&self) -> bool70     fn eof(&self) -> bool {
71         self.reader.eof()
72     }
original_position(&self) -> usize73     fn original_position(&self) -> usize {
74         MemorySectionReader::original_position(self)
75     }
76 }
77 
78 impl<'a> SectionWithLimitedItems for MemorySectionReader<'a> {
get_count(&self) -> u3279     fn get_count(&self) -> u32 {
80         MemorySectionReader::get_count(self)
81     }
82 }
83 
84 impl<'a> IntoIterator for MemorySectionReader<'a> {
85     type Item = Result<MemoryType>;
86     type IntoIter = SectionIteratorLimited<MemorySectionReader<'a>>;
87 
into_iter(self) -> Self::IntoIter88     fn into_iter(self) -> Self::IntoIter {
89         SectionIteratorLimited::new(self)
90     }
91 }
92