1use super::*;
4use std::convert::*;
5
6pub(crate) fn read_pool_visitor(read: &mut impl Read, visitor: &mut impl Visitor) -> io::Result<()> {
7 let count = read_u2(read)?;
8 visitor.on_unused(0, UnusedPlaceholder {});
9 let mut index = 1; while index < count {
11 let tag = Type::from(read_u1(read)?);
12 match tag {
13 Class::TAG => visitor.on_class(index, Class::read_after_tag(read)?),
14 Fieldref::TAG => visitor.on_field(index, Fieldref::read_after_tag(read)?),
15 Methodref::TAG => visitor.on_method(index, Methodref::read_after_tag(read)?),
16 InterfaceMethodref::TAG => visitor.on_interface_method(index, InterfaceMethodref::read_after_tag(read)?),
17 String::TAG => visitor.on_string(index, String::read_after_tag(read)?),
18 Integer::TAG => visitor.on_integer(index, Integer::read_after_tag(read)?),
19 Float::TAG => visitor.on_float(index, Float::read_after_tag(read)?),
20 Long::TAG => {
21 visitor.on_long(index, Long::read_after_tag(read)?);
22 index += 1;
23 visitor.on_unused(index, UnusedPlaceholder {});
24 },
25 Double::TAG => {
26 visitor.on_double(index, Double::read_after_tag(read)?);
27 index += 1;
28 visitor.on_unused(index, UnusedPlaceholder {});
29 },
30 NameAndType::TAG => visitor.on_name_and_tag(index, NameAndType::read_after_tag(read)?),
31 Utf8::TAG => visitor.on_utf8(index, Utf8::read_after_tag(read)?),
32 MethodHandle::TAG => visitor.on_method_handle(index, MethodHandle::read_after_tag(read)?),
33 MethodType::TAG => visitor.on_method_type(index, MethodType::read_after_tag(read)?),
34 InvokeDynamic::TAG => visitor.on_invoke_dynamic(index, InvokeDynamic::read_after_tag(read)?),
35 _ => {
36 return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Expected CONSTANT_* value reading constant pool, got {:?}", tag)));
37 },
38 }
39 index += 1;
40 }
41 Ok(())
42}
43
44
45
46#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
48#[repr(transparent)] pub struct Type(u8);
49
50impl From<u8> for Type {
51 fn from(value: u8) -> Self {
52 Self(value)
53 }
54}
55
56
57
58pub trait Visitor {
60 fn on_unused (&mut self, _index: u16, _unused: UnusedPlaceholder) {}
61 fn on_class (&mut self, _index: u16, _class: Class) {}
62 fn on_field (&mut self, _index: u16, _field: Fieldref) {}
63 fn on_method (&mut self, _index: u16, _method: Methodref) {}
64 fn on_interface_method (&mut self, _index: u16, _interface_method: InterfaceMethodref) {}
65 fn on_string (&mut self, _index: u16, _string: String) {}
66 fn on_integer (&mut self, _index: u16, _integer: Integer) {}
67 fn on_float (&mut self, _index: u16, _float: Float) {}
68 fn on_long (&mut self, _index: u16, _long: Long) {}
69 fn on_double (&mut self, _index: u16, _double: Double) {}
70 fn on_name_and_tag (&mut self, _index: u16, _name_and_tag: NameAndType) {}
71 fn on_utf8 (&mut self, _index: u16, _utf8: Utf8) {}
72 fn on_method_handle (&mut self, _index: u16, _method_handle: MethodHandle) {}
73 fn on_method_type (&mut self, _index: u16, _method_type: MethodType) {}
74 fn on_invoke_dynamic (&mut self, _index: u16, _invoke_dynamic: InvokeDynamic) {}
75}
76
77
78
79#[derive(Clone, Debug)] pub struct UnusedPlaceholder {}
81#[derive(Clone, Debug)] pub struct Class { pub name_index: u16 }
83#[derive(Clone, Debug)] pub struct Fieldref { pub class_index: u16, pub name_and_type_index: u16 }
85#[derive(Clone, Debug)] pub struct Methodref { pub class_index: u16, pub name_and_type_index: u16 }
87#[derive(Clone, Debug)] pub struct InterfaceMethodref { pub class_index: u16, pub name_and_type_index: u16 }
89#[derive(Clone, Debug)] pub struct String { pub string_index: u16 }
91#[derive(Clone, Debug)] pub struct Integer ( pub i32 );
93#[derive(Clone, Debug)] pub struct Float ( pub f32 );
95#[derive(Clone, Debug)] pub struct Long ( pub i64 ); #[derive(Clone, Debug)] pub struct Double ( pub f64 ); #[derive(Clone, Debug)] pub struct NameAndType { pub name_index: u16, pub descriptor_index: u16 }
101#[derive(Clone, Debug)] pub struct Utf8 ( pub std::string::String ); #[derive(Clone, Debug)] pub struct MethodHandle { pub reference_kind: u8, pub reference_index: u16 }
105#[derive(Clone, Debug)] pub struct MethodType { pub descriptor_index: u16 }
107#[derive(Clone, Debug)] pub struct InvokeDynamic { pub bootstrap_method_attr_index: u16, pub name_and_type_index: u16 }
109
110impl Class { pub const TAG : Type = Type( 7); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ name_index: read_u2(r)? })} }
111impl Fieldref { pub const TAG : Type = Type( 9); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ class_index: read_u2(r)?, name_and_type_index: read_u2(r)? })} }
112impl Methodref { pub const TAG : Type = Type(10); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ class_index: read_u2(r)?, name_and_type_index: read_u2(r)? })} }
113impl InterfaceMethodref { pub const TAG : Type = Type(11); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ class_index: read_u2(r)?, name_and_type_index: read_u2(r)? })} }
114impl String { pub const TAG : Type = Type( 8); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ string_index: read_u2(r)? })} }
115impl Integer { pub const TAG : Type = Type( 3); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self( read_i4(r)? ))} }
116impl Float { pub const TAG : Type = Type( 4); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self( f32::from_bits(read_u4(r)?) ))} }
117impl Long { pub const TAG : Type = Type( 5); pub const ENTRIES : u8 = 2; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self( read_i8(r)? ))} }
118impl Double { pub const TAG : Type = Type( 6); pub const ENTRIES : u8 = 2; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self( f64::from_bits(read_u8(r)?) ))} }
119impl NameAndType { pub const TAG : Type = Type(12); pub const ENTRIES : u8 = 2; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ name_index: read_u2(r)?, descriptor_index: read_u2(r)? })} }
120impl Utf8 { pub const TAG : Type = Type( 1); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> { Self::read_after_tag_impl(r) } }
121impl MethodHandle { pub const TAG : Type = Type(15); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ reference_kind: read_u1(r)?, reference_index: read_u2(r)? })} }
122impl MethodType { pub const TAG : Type = Type(16); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ descriptor_index: read_u2(r)? })} }
123impl InvokeDynamic { pub const TAG : Type = Type(18); pub const ENTRIES : u8 = 1; pub fn read_after_tag(r: &mut impl Read) -> io::Result<Self> {Ok(Self{ bootstrap_method_attr_index: read_u2(r)?, name_and_type_index: read_u2(r)? })} }
124
125impl Utf8 {
127 fn expect_byte(remaining: &[u8], index: usize, mask: u8, equal: u8) -> io::Result<u8> {
128 if let Some(&value) = remaining.get(index) {
129 if value & mask == equal {
130 return Ok(value & !mask);
131 } else {
132 Err(io::Error::new(io::ErrorKind::InvalidData, format!("Invalid 'UTF8' string - expected index {} byte {:b} & mask {:b} == {:b}", index, value, mask, equal)))
133 }
134 } else {
135 Err(io::Error::new(io::ErrorKind::InvalidData, "Incomplete 'UTF8' string - expected more bytes"))
136 }
137 }
138
139 fn read_char(remaining: &mut &[u8]) -> io::Result<char> {
140 if let Some(&b0) = remaining.get(0) {
141 if b0 & 0b10000000 == 0b00000000 {
142 *remaining = &remaining[1..];
144 Ok(b0 as char)
145
146 } else if b0 & 0b11100000 == 0b11000000 {
147 let b0 = (b0 & 0b00011111) as u32;
149 let b1 = Self::expect_byte(*remaining, 1, 0b11000000, 0b10000000)? as u32;
150 *remaining = &remaining[2..];
151 Ok(char::try_from(b0 << 6 | b1 << 0).map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Expected 'UTF8' bytes"))?)
152
153 } else if b0 == 0b11011101 {
154 let a = Self::expect_byte(*remaining, 1, 0b11110000, 0b10100000)? as u32; let b = Self::expect_byte(*remaining, 2, 0b11000000, 0b10000000)? as u32; let _ = Self::expect_byte(*remaining, 3, 0b11111111, 0b11101101)? as u32; let c = Self::expect_byte(*remaining, 4, 0b11110000, 0b10110000)? as u32; let d = Self::expect_byte(*remaining, 5, 0b11000000, 0b10000000)? as u32; *remaining = &remaining[6..];
161 Ok(char::try_from(a << 16 | b << 10 | c << 6 | d << 0).map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Expected 'UTF8' bytes"))?)
162
163 } else if b0 & 0b11110000 == 0b11100000 {
164 let b0 = (b0 & 0b00001111) as u32;
166 let b1 = Self::expect_byte(*remaining, 1, 0b11000000, 0b10000000)? as u32;
167 let b2 = Self::expect_byte(*remaining, 2, 0b11000000, 0b10000000)? as u32;
168 *remaining = &remaining[3..];
169 Ok(char::try_from(b0 << 12 | b1 << 6 | b2 << 0).map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Expected 'UTF8' bytes"))?)
170
171 } else {
172 Err(io::Error::new(io::ErrorKind::InvalidData, "Expected 'UTF8' bytes, invalid starting byte"))
173 }
174 } else {
175 Err(io::Error::new(io::ErrorKind::InvalidData, "Expected 'UTF8' bytes"))
176 }
177 }
178
179 fn read_after_tag_impl(r: &mut impl Read) -> io::Result<Self> {
180 let bytes = read_u2(r)? as usize;
181 let mut buffer = Vec::new();
182 buffer.resize(bytes, 0u8);
183 r.read_exact(&mut buffer[..])?;
184 let mut remaining = &buffer[..];
185 let mut output = std::string::String::new();
186 while !remaining.is_empty() {
187 output.push(Self::read_char(&mut remaining)?);
188 }
189 Ok(Self(output))
190 }
191}
192
193
194
195#[derive(Clone, Debug)]
197pub enum Constant {
198 UnusedPlaceholder(UnusedPlaceholder),
200 Class(Class),
202 Fieldref(Fieldref),
204 Methodref(Methodref),
206 InterfaceMethodref(InterfaceMethodref),
208 String(String),
210 Integer(Integer),
212 Float(Float),
214 Long(Long),
216 Double(Double),
218 NameAndType(NameAndType),
220 Utf8(Utf8),
222 MethodHandle(MethodHandle),
224 MethodType(MethodType),
226 InvokeDynamic(InvokeDynamic),
228}
229
230impl From<UnusedPlaceholder> for Constant { fn from(value: UnusedPlaceholder ) -> Self { Constant::UnusedPlaceholder(value) } }
231impl From<Class> for Constant { fn from(value: Class ) -> Self { Constant::Class(value) } }
232impl From<Fieldref> for Constant { fn from(value: Fieldref ) -> Self { Constant::Fieldref(value) } }
233impl From<Methodref> for Constant { fn from(value: Methodref ) -> Self { Constant::Methodref(value) } }
234impl From<InterfaceMethodref> for Constant { fn from(value: InterfaceMethodref ) -> Self { Constant::InterfaceMethodref(value) } }
235impl From<String> for Constant { fn from(value: String ) -> Self { Constant::String(value) } }
236impl From<Integer> for Constant { fn from(value: Integer ) -> Self { Constant::Integer(value) } }
237impl From<Float> for Constant { fn from(value: Float ) -> Self { Constant::Float(value) } }
238impl From<Long> for Constant { fn from(value: Long ) -> Self { Constant::Long(value) } }
239impl From<Double> for Constant { fn from(value: Double ) -> Self { Constant::Double(value) } }
240impl From<NameAndType> for Constant { fn from(value: NameAndType ) -> Self { Constant::NameAndType(value) } }
241impl From<Utf8> for Constant { fn from(value: Utf8 ) -> Self { Constant::Utf8(value) } }
242impl From<MethodHandle> for Constant { fn from(value: MethodHandle ) -> Self { Constant::MethodHandle(value) } }
243impl From<MethodType> for Constant { fn from(value: MethodType ) -> Self { Constant::MethodType(value) } }
244impl From<InvokeDynamic> for Constant { fn from(value: InvokeDynamic ) -> Self { Constant::InvokeDynamic(value) } }