/rust/registry/src/index.crates.io-6f17d22bba15001f/wasmparser-0.121.2/src/readers/core/names.rs
Line | Count | Source (jump to first uncovered line) |
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::{ |
17 | | BinaryReader, BinaryReaderError, FromReader, Result, SectionLimited, Subsection, Subsections, |
18 | | }; |
19 | | use std::ops::Range; |
20 | | |
21 | | /// Represents a name map from the names custom section. |
22 | | pub type NameMap<'a> = SectionLimited<'a, Naming<'a>>; |
23 | | |
24 | | /// Represents a name for an index from the names section. |
25 | | #[derive(Debug, Copy, Clone)] |
26 | | pub struct Naming<'a> { |
27 | | /// The index being named. |
28 | | pub index: u32, |
29 | | /// The name for the index. |
30 | | pub name: &'a str, |
31 | | } |
32 | | |
33 | | impl<'a> FromReader<'a> for Naming<'a> { |
34 | 0 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
35 | 0 | let index = reader.read_var_u32()?; |
36 | 0 | let name = reader.read_string()?; |
37 | 0 | Ok(Naming { index, name }) |
38 | 0 | } |
39 | | } |
40 | | |
41 | | /// Represents a reader for indirect names from the names custom section. |
42 | | pub type IndirectNameMap<'a> = SectionLimited<'a, IndirectNaming<'a>>; |
43 | | |
44 | | /// Represents an indirect name in the names custom section. |
45 | | #[derive(Debug, Clone)] |
46 | | pub struct IndirectNaming<'a> { |
47 | | /// The indirect index of the name. |
48 | | pub index: u32, |
49 | | /// The map of names within the `index` prior. |
50 | | pub names: NameMap<'a>, |
51 | | } |
52 | | |
53 | | impl<'a> FromReader<'a> for IndirectNaming<'a> { |
54 | 0 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
55 | 0 | let index = reader.read_var_u32()?; |
56 | | |
57 | | // Skip the `NameMap` manually here. |
58 | | // |
59 | | // FIXME(#188) shouldn't need to skip here |
60 | 0 | let names = reader.skip(|reader| { |
61 | 0 | let count = reader.read_var_u32()?; |
62 | 0 | for _ in 0..count { |
63 | 0 | reader.read_var_u32()?; |
64 | 0 | reader.skip_string()?; |
65 | | } |
66 | 0 | Ok(()) |
67 | 0 | })?; |
68 | | |
69 | | Ok(IndirectNaming { |
70 | 0 | index, |
71 | 0 | names: NameMap::new(names.remaining_buffer(), names.original_position())?, |
72 | | }) |
73 | 0 | } |
74 | | } |
75 | | |
76 | | /// Represents a name read from the names custom section. |
77 | | #[derive(Clone)] |
78 | | pub enum Name<'a> { |
79 | | /// The name is for the module. |
80 | | Module { |
81 | | /// The specified name. |
82 | | name: &'a str, |
83 | | /// The byte range that `name` occupies in the original binary. |
84 | | name_range: Range<usize>, |
85 | | }, |
86 | | /// The name is for the functions. |
87 | | Function(NameMap<'a>), |
88 | | /// The name is for the function locals. |
89 | | Local(IndirectNameMap<'a>), |
90 | | /// The name is for the function labels. |
91 | | Label(IndirectNameMap<'a>), |
92 | | /// The name is for the types. |
93 | | Type(NameMap<'a>), |
94 | | /// The name is for the tables. |
95 | | Table(NameMap<'a>), |
96 | | /// The name is for the memories. |
97 | | Memory(NameMap<'a>), |
98 | | /// The name is for the globals. |
99 | | Global(NameMap<'a>), |
100 | | /// The name is for the element segments. |
101 | | Element(NameMap<'a>), |
102 | | /// The name is for the data segments. |
103 | | Data(NameMap<'a>), |
104 | | /// The name is for tags. |
105 | | Tag(NameMap<'a>), |
106 | | /// An unknown [name subsection](https://webassembly.github.io/spec/core/appendix/custom.html#subsections). |
107 | | Unknown { |
108 | | /// The identifier for this subsection. |
109 | | ty: u8, |
110 | | /// The contents of this subsection. |
111 | | data: &'a [u8], |
112 | | /// The range of bytes, relative to the start of the original data |
113 | | /// stream, that the contents of this subsection reside in. |
114 | | range: Range<usize>, |
115 | | }, |
116 | | } |
117 | | |
118 | | /// A reader for the name custom section of a WebAssembly module. |
119 | | pub type NameSectionReader<'a> = Subsections<'a, Name<'a>>; |
120 | | |
121 | | impl<'a> Subsection<'a> for Name<'a> { |
122 | 0 | fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> { |
123 | 0 | let data = reader.remaining_buffer(); |
124 | 0 | let offset = reader.original_position(); |
125 | 0 | Ok(match id { |
126 | | 0 => { |
127 | 0 | let name = reader.read_string()?; |
128 | 0 | if !reader.eof() { |
129 | 0 | return Err(BinaryReaderError::new( |
130 | 0 | "trailing data at the end of a name", |
131 | 0 | reader.original_position(), |
132 | 0 | )); |
133 | 0 | } |
134 | 0 | Name::Module { |
135 | 0 | name, |
136 | 0 | name_range: offset..offset + reader.position, |
137 | 0 | } |
138 | | } |
139 | 0 | 1 => Name::Function(NameMap::new(data, offset)?), |
140 | 0 | 2 => Name::Local(IndirectNameMap::new(data, offset)?), |
141 | 0 | 3 => Name::Label(IndirectNameMap::new(data, offset)?), |
142 | 0 | 4 => Name::Type(NameMap::new(data, offset)?), |
143 | 0 | 5 => Name::Table(NameMap::new(data, offset)?), |
144 | 0 | 6 => Name::Memory(NameMap::new(data, offset)?), |
145 | 0 | 7 => Name::Global(NameMap::new(data, offset)?), |
146 | 0 | 8 => Name::Element(NameMap::new(data, offset)?), |
147 | 0 | 9 => Name::Data(NameMap::new(data, offset)?), |
148 | 0 | 11 => Name::Tag(NameMap::new(data, offset)?), |
149 | 0 | ty => Name::Unknown { |
150 | 0 | ty, |
151 | 0 | data, |
152 | 0 | range: offset..offset + data.len(), |
153 | 0 | }, |
154 | | }) |
155 | 0 | } |
156 | | } |