wasm/core/reader/
section_header.rs

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