wasm/core/reader/
section_header.rs1use crate::core::reader::span::Span;
2use crate::core::reader::WasmReader;
3use crate::core::utils::ToUsizeExt;
4use crate::ValidationError;
5
6#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
7pub enum SectionTy {
8 Custom = 0,
9 Type = 1,
10 Import = 2,
11 Function = 3,
12 Table = 4,
13 Memory = 5,
14 Global = 6,
15 Export = 7,
16 Start = 8,
17 Element = 9,
18 Code = 10,
19 Data = 11,
20 DataCount = 12,
21}
22
23impl SectionTy {
24 pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
25 use SectionTy::*;
26 let ty = match wasm.read_u8()? {
27 0 => Custom,
28 1 => Type,
29 2 => Import,
30 3 => Function,
31 4 => Table,
32 5 => Memory,
33 6 => Global,
34 7 => Export,
35 8 => Start,
36 9 => Element,
37 10 => Code,
38 11 => Data,
39 12 => DataCount,
40 other => return Err(ValidationError::MalformedSectionTypeDiscriminator(other)),
41 };
42
43 Ok(ty)
44 }
45}
46
47#[derive(Debug)]
48pub(crate) struct SectionHeader {
49 pub ty: SectionTy,
50 pub contents: Span,
51}
52
53impl SectionHeader {
54 pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
55 let ty = SectionTy::read(wasm)?;
56 let size: u32 = wasm.read_var_u32()?;
57 let contents_span = wasm.make_span(size.into_usize())?;
58
59 Ok(SectionHeader {
60 ty,
61 contents: contents_span,
62 })
63 }
64}