1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
extern crate anyhow;
extern crate bytes;

use self::anyhow::{anyhow, Result};
use self::bytes::Bytes;

use crate::attributes::Attributes;
use crate::classfile::{
    Addressed, ClassFile, Field, Fields, Interfaces, MetaData, Method, Methods,
};
use crate::constants::MAGIC;
use crate::flags::{ClassFileAccessFlags, FieldAccessFlags, MethodAccessFlags};
use crate::pool::{
    ConstantClass, ConstantDouble, ConstantDynamic, ConstantEntry, ConstantField, ConstantFloat,
    ConstantInteger, ConstantInterfaceMethod, ConstantInvokeDynamic, ConstantLong, ConstantMethod,
    ConstantMethodHandle, ConstantMethodType, ConstantNameAndType, ConstantPool, ConstantString,
    ConstantTag, ConstantUtf8,
};
use crate::result::ParseResult;
use support::bytes_ext::SafeBuf;

pub struct Parser {
    bytes: Bytes,
}

impl Parser {
    pub fn new(data: &[u8]) -> Self {
        Self {
            bytes: Bytes::copy_from_slice(data),
        }
    }

    fn parse_constant_pool(&mut self) -> Result<ConstantPool> {
        let length = self.bytes.try_get_u16()?;
        let mut pool = ConstantPool::new();

        let mut i = 0;
        while i < (length - 1) {
            let tag = ConstantTag::from_tag(self.bytes.try_get_u8()?);
            let entry = match tag {
                ConstantTag::Class => ConstantEntry::Class(ConstantClass {
                    tag: ConstantTag::Class,
                    name: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::Field => ConstantEntry::Field(ConstantField {
                    tag: ConstantTag::Field,
                    class: pool.address(self.bytes.try_get_u16()?),
                    name_and_type: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::Method => ConstantEntry::Method(ConstantMethod {
                    tag: ConstantTag::Method,
                    class: pool.address(self.bytes.try_get_u16()?),
                    name_and_type: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::InterfaceMethod => {
                    ConstantEntry::InterfaceMethod(ConstantInterfaceMethod {
                        tag: ConstantTag::InterfaceMethod,
                        class: pool.address(self.bytes.try_get_u16()?),
                        name_and_type: pool.address(self.bytes.try_get_u16()?),
                    })
                }
                ConstantTag::String => ConstantEntry::String(ConstantString {
                    tag: ConstantTag::String,
                    string: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::Integer => ConstantEntry::Integer(ConstantInteger {
                    tag: ConstantTag::Integer,
                    bytes: self.bytes.try_get_u32()?,
                }),
                ConstantTag::Float => ConstantEntry::Float(ConstantFloat {
                    tag: ConstantTag::Float,
                    bytes: self.bytes.try_get_f32()?,
                }),
                ConstantTag::Long => ConstantEntry::Long(ConstantLong {
                    tag: ConstantTag::Long,
                    bytes: self.bytes.try_get_u64()?,
                }),
                ConstantTag::Double => ConstantEntry::Double(ConstantDouble {
                    tag: ConstantTag::Float,
                    bytes: self.bytes.try_get_f64()?,
                }),
                ConstantTag::NameAndType => ConstantEntry::NameAndType(ConstantNameAndType {
                    tag: ConstantTag::NameAndType,
                    name: pool.address(self.bytes.try_get_u16()?),
                    descriptor: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::Utf8 => {
                    let length = self.bytes.try_get_u16()?;
                    let mut bytes: Vec<u8> = Vec::with_capacity(length.into());

                    for _ in 0..length {
                        bytes.push(self.bytes.try_get_u8()?);
                    }

                    ConstantEntry::Utf8(ConstantUtf8 {
                        tag: ConstantTag::Utf8,
                        length,
                        bytes,
                    })
                }
                ConstantTag::MethodHandle => ConstantEntry::MethodHandle(ConstantMethodHandle {
                    tag: ConstantTag::MethodHandle,
                    kind: self.bytes.try_get_u8()?,
                    index: self.bytes.try_get_u16()?,
                }),
                ConstantTag::MethodType => ConstantEntry::MethodType(ConstantMethodType {
                    tag: ConstantTag::MethodType,
                    descriptor: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::Dynamic => ConstantEntry::Dynamic(ConstantDynamic {
                    tag: ConstantTag::Dynamic,
                    method_index: self.bytes.try_get_u16()?,
                    name_and_type: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::InvokeDynamic => ConstantEntry::InvokeDynamic(ConstantInvokeDynamic {
                    tag: ConstantTag::InvokeDynamic,
                    method_index: self.bytes.try_get_u16()?,
                    name_and_type: pool.address(self.bytes.try_get_u16()?),
                }),
                ConstantTag::Module => todo!(),
                ConstantTag::Package => todo!(),
            };

            let should_reserve_next =
                matches!(entry, ConstantEntry::Long(_) | ConstantEntry::Double(_));
            pool.insert(entry);

            // Special case: 64 Bit types are supposed to take up 2 slots
            // So, insert a dummy and increment the index by an additional 1
            if should_reserve_next {
                pool.insert(ConstantEntry::Reserved);
                i += 1;
            }

            i += 1;
        }

        Ok(pool)
    }

    fn parse_interfaces(&mut self, pool: &ConstantPool) -> Result<Interfaces> {
        let length = self.bytes.try_get_u16()?;
        let mut interfaces = Interfaces {
            values: Vec::with_capacity(length.into()),
        };

        for _ in 0..length {
            interfaces
                .values
                .push(pool.address(self.bytes.try_get_u16()?));
        }

        Ok(interfaces)
    }

    fn parse_fields(&mut self, pool: &ConstantPool) -> Result<Fields> {
        let length = self.bytes.try_get_u16()?;
        let mut fields = Fields {
            values: Vec::with_capacity(length.into()),
        };

        for _ in 0..length {
            fields.values.push(Field {
                flags: FieldAccessFlags::from_bits(self.bytes.try_get_u16()?)?,
                name: pool.address(self.bytes.try_get_u16()?),
                descriptor: pool.address(self.bytes.try_get_u16()?),
                attributes: Attributes::parse(&mut self.bytes, pool)?,
            });
        }

        Ok(fields)
    }

    fn parse_methods(&mut self, pool: &ConstantPool) -> Result<Methods> {
        let length = self.bytes.try_get_u16()?;
        let mut methods = Methods {
            values: Vec::with_capacity(length.into()),
        };

        for _ in 0..length {
            methods.values.push(Method {
                flags: MethodAccessFlags::from_bits(self.bytes.try_get_u16()?)?,
                name: pool.address(self.bytes.try_get_u16()?),
                descriptor: pool.address(self.bytes.try_get_u16()?),
                attributes: Attributes::parse(&mut self.bytes, pool)?,
            });
        }

        Ok(methods)
    }

    pub fn parse(&mut self) -> ParseResult {
        let magic = self.bytes.try_get_u32()?;

        // Format checking: The first four bytes must contain the right magic number
        if magic != MAGIC {
            return Err(anyhow!("invalid magic value '{}'", magic));
        }

        let minor = self.bytes.try_get_u16()?;
        let major = self.bytes.try_get_u16()?;

        let meta_data = MetaData {
            minor_version: minor,
            major_version: major,
        };

        let constant_pool = self.parse_constant_pool()?;
        // Format checking: The constant pool must satisfy the constraints documented throughout §4.4.
        // All field references and method references in the constant pool must have valid
        // names, valid classes, and valid descriptors (§4.3).
        constant_pool.perform_format_checking()?;

        let access_flags = ClassFileAccessFlags::from_bits(self.bytes.try_get_u16()?)?;
        let this_class: Addressed<ConstantClass> = constant_pool.address(self.bytes.try_get_u16()?);

        let super_class_index = self.bytes.try_get_u16()?;
        let mut super_class: Option<Addressed<ConstantClass>> = None;
        if super_class_index != 0 {
            super_class = Some(constant_pool.address(super_class_index));
        }

        let interfaces = self.parse_interfaces(&constant_pool)?;
        let fields = self.parse_fields(&constant_pool)?;
        let methods = self.parse_methods(&constant_pool)?;
        let attributes = Attributes::parse(&mut self.bytes, &constant_pool)?;

        // Format checking: The class file must not be truncated or have extra bytes at the end
        if !self.bytes.is_empty() {
            return Err(anyhow!("classfile has extra bytes at the end"));
        }

        Ok(ClassFile {
            constant_pool,
            meta_data,
            access_flags,
            this_class,
            super_class,
            interfaces,
            fields,
            methods,
            attributes,
        })
    }
}