| 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 crate::{BinaryReader, FromReader, MemoryType, Result, SectionLimited}; |
| 17 |
|
| 18 |
/// A reader for the memory section of a WebAssembly module. |
| 19 |
pub type MemorySectionReader<'a> = SectionLimited<'a, MemoryType>; |
| 20 |
|
| 21 |
impl<'a> FromReader<'a> for MemoryType { |
| 22 |
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
| 23 |
let pos = reader.original_position(); |
| 24 |
let flags = reader.read_u8()?; |
| 25 |
|
| 26 |
if (flags & !0b1111) != 0 { |
| 27 |
bail!(pos, "invalid memory limits flags"); |
| 28 |
} |
| 29 |
|
| 30 |
let memory64 = flags & 0b0100 != 0; |
| 31 |
let shared = flags & 0b0010 != 0; |
| 32 |
let has_max = flags & 0b0001 != 0; |
| 33 |
let has_page_size = flags & 0b1000 != 0; |
| 34 |
|
| 35 |
Ok(MemoryType { |
| 36 |
memory64, |
| 37 |
shared, |
| 38 |
initial: if reader.memory64() { |
| 39 |
reader.read_var_u64()? |
| 40 |
} else { |
| 41 |
reader.read_var_u32()?.into() |
| 42 |
}, |
| 43 |
maximum: if !has_max { |
| 44 |
None |
| 45 |
} else if reader.memory64() { |
| 46 |
Some(reader.read_var_u64()?) |
| 47 |
} else { |
| 48 |
Some(reader.read_var_u32()?.into()) |
| 49 |
}, |
| 50 |
page_size_log2: if has_page_size { |
| 51 |
let val = reader.read_var_u32()?; |
| 52 |
if val >= 64 { |
| 53 |
bail!(pos, "invalid custom page size"); |
| 54 |
} |
| 55 |
Some(val) |
| 56 |
} else { |
| 57 |
None |
| 58 |
}, |
| 59 |
}) |
| 60 |
} |
| 61 |
} |