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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::From;
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io::Read;

mod mutf8 {
    pub enum MUtf8Error {
        MissingByte,
        UnknownByte,
        InvalidChar,
    }

    pub fn to_string<T: IntoIterator<Item = u8>>(bytes: T) -> Result<String, MUtf8Error> {
        let mut s = String::new();
        let mut iterator = bytes.into_iter();
        loop {
            if let Some(b) = iterator.next() {
                if b == 0b1110_1101 {
                    let b2 = iterator.next().ok_or(MUtf8Error::MissingByte)?;
                    if !b2 & 0b1111_0000 == 0b1010_0000 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    let b3 = iterator.next().ok_or(MUtf8Error::MissingByte)?;
                    if !b3 & 0b1100_0000 == 0b1000_0000 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    if !iterator.next().ok_or(MUtf8Error::MissingByte)? & 0xFF == 0b1110_1101 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    let b4 = iterator.next().ok_or(MUtf8Error::MissingByte)?;
                    if !b4 & 0b1111_0000 == 0b1011_0000 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    let b5 = iterator.next().ok_or(MUtf8Error::MissingByte)?;
                    if !b5 & 0b1100_0000 == 0b1000_0000 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    let codepoint: u32 = 0x10000
                        + ((b2 as u32 & 0x0f) << 16)
                        + ((b3 as u32 & 0x3f) << 10)
                        + ((b4 as u32 & 0x0f) << 6)
                        + (b5 as u32 & 0x3f);
                    s.push(std::char::from_u32(codepoint).ok_or(MUtf8Error::InvalidChar)?);
                } else if b & 0b1111_0000 == 0b1110_0000 {
                    let b2 = iterator.next().ok_or(MUtf8Error::MissingByte)?;
                    if !b2 & 0b1100_0000 == 0b1000_0000 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    let b3 = iterator.next().ok_or(MUtf8Error::MissingByte)?;
                    if !b3 & 0b1100_0000 == 0b1000_0000 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    let codepoint: u32 = b3 as u32 & 0b11_1111
                        | ((b2 as u32 & 0b11_1111) << 6)
                        | ((b as u32 & 0b1_1111) << 12);
                    s.push(std::char::from_u32(codepoint).ok_or(MUtf8Error::InvalidChar)?);
                } else if b & 0b1110_0000 == 0b1100_0000 {
                    let b2 = iterator.next().ok_or(MUtf8Error::MissingByte)?;
                    if !b2 & 0b1100_0000 == 0b1000_0000 {
                        return Err(MUtf8Error::UnknownByte);
                    }
                    let codepoint: u32 = b2 as u32 & 0b11_1111 | ((b as u32 & 0b1_1111) << 6);
                    s.push(std::char::from_u32(codepoint).ok_or(MUtf8Error::InvalidChar)?);
                } else if b & 0b1000_0000 == 0 {
                    s.push(b as char);
                } else {
                    return Err(MUtf8Error::UnknownByte);
                }
            } else {
                break;
            }
        }
        Ok(s)
    }
}

#[derive(Debug)]
pub enum ClassFileError {
    InvalidMagic,
    Read,
    InvalidCPType,
    InvalidCPEntry,
    MUtf8Format,
    EndOfFile,
    MoreData,
}

impl From<std::io::Error> for ClassFileError {
    fn from(_: std::io::Error) -> Self {
        ClassFileError::Read
    }
}

impl From<mutf8::MUtf8Error> for ClassFileError {
    fn from(_: mutf8::MUtf8Error) -> Self {
        ClassFileError::MUtf8Format
    }
}

impl Error for ClassFileError {}

impl Display for ClassFileError {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(
            f,
            "{}",
            match self {
                ClassFileError::InvalidMagic => "invalid magic value",
                ClassFileError::Read => "error reading input",
                ClassFileError::InvalidCPType => "invalid constant pool type",
                ClassFileError::InvalidCPEntry => "invalid index into constant pool",
                ClassFileError::MUtf8Format => "error in mutf8 format",
                ClassFileError::EndOfFile => "end of file",
                ClassFileError::MoreData => "more data after expected end of file",
            }
        )
    }
}

fn read_u8<T: Read>(data: &mut T) -> Result<u8, ClassFileError> {
    let mut buf = [0_u8; 1];
    let amt = data.read(&mut buf)?;
    if amt < 1 {
        return Err(ClassFileError::EndOfFile);
    }
    Ok(buf[0])
}

fn read_u16<T: Read>(data: &mut T) -> Result<u16, ClassFileError> {
    let mut buf = [0_u8; 2];
    let amt = data.read(&mut buf)?;
    if amt < 2 {
        return Err(ClassFileError::EndOfFile);
    }
    let r: u16 = unsafe { std::mem::transmute(buf) };
    Ok(r.to_be())
}

fn read_u32<T: Read>(data: &mut T) -> Result<u32, ClassFileError> {
    let mut buf = [0_u8; 4];
    let amt = data.read(&mut buf)?;
    if amt < 4 {
        return Err(ClassFileError::EndOfFile);
    }
    let r: u32 = unsafe { std::mem::transmute(buf) };
    Ok(r.to_be())
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum ConstantPoolInfo {
    Class {
        name_index: u16,
    },
    Fieldref {
        class_index: u16,
        name_and_type_index: u16,
    },
    Methodref {
        class_index: u16,
        name_and_type_index: u16,
    },
    InterfaceMethodref {
        class_index: u16,
        name_and_type_index: u16,
    },
    String {
        string_index: u16,
    },
    Integer {
        data: i32,
    },
    Float {
        data: f32,
    },
    Long {
        data: i64,
    },
    Double {
        data: f64,
    },
    NameAndType {
        name_index: u16,
        descriptor_index: u16,
    },
    Utf8 {
        length: u16,
        string: String,
    },
    MethodHandle {
        reference_kind: u8,
        reference_index: u16,
    },
    MethodType {
        descriptor_index: u16,
    },
    InvokeDynamic {
        bootstrap_method_attr_index: u16,
        name_and_type_index: u16,
    },
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ConstantPool {
    data: HashMap<u16, ConstantPoolInfo>,
}

impl ConstantPool {
    pub fn get_entry(&self, index: u16) -> Result<ConstantPoolInfo, ClassFileError> {
        Ok(self
            .data
            .get(&index)
            .ok_or(ClassFileError::InvalidCPEntry)?
            .clone())
    }

    pub fn get_utf8_entry(&self, index: u16) -> Result<String, ClassFileError> {
        if let ConstantPoolInfo::Utf8 { length: _, string } = self.get_entry(index)? {
            Ok(string)
        } else {
            Err(ClassFileError::InvalidCPEntry)
        }
    }
}

fn read_constant_pool<T: Read>(data: &mut T) -> Result<ConstantPool, ClassFileError> {
    let constant_pool_count = read_u16(data)?;
    let mut constant_pool = HashMap::new();
    let mut i = 1;
    while i < constant_pool_count {
        let cp_type = read_u8(data)?;
        let entry = match cp_type {
            7 => ConstantPoolInfo::Class {
                name_index: read_u16(data)?,
            },
            9 => ConstantPoolInfo::Fieldref {
                class_index: read_u16(data)?,
                name_and_type_index: read_u16(data)?,
            },
            10 => ConstantPoolInfo::Methodref {
                class_index: read_u16(data)?,
                name_and_type_index: read_u16(data)?,
            },
            11 => ConstantPoolInfo::InterfaceMethodref {
                class_index: read_u16(data)?,
                name_and_type_index: read_u16(data)?,
            },
            8 => ConstantPoolInfo::String {
                string_index: read_u16(data)?,
            },
            3 => ConstantPoolInfo::Integer {
                data: unsafe { std::mem::transmute(read_u32(data)?) },
            },
            4 => ConstantPoolInfo::Float {
                data: unsafe { std::mem::transmute(read_u32(data)?) },
            },
            5 => {
                let high = read_u32(data)?;
                let low = read_u32(data)?;
                ConstantPoolInfo::Long {
                    data: unsafe { std::mem::transmute([low, high]) },
                }
            }
            6 => {
                let high = read_u32(data)?;
                let low = read_u32(data)?;
                ConstantPoolInfo::Double {
                    data: unsafe { std::mem::transmute([low, high]) },
                }
            }
            12 => ConstantPoolInfo::NameAndType {
                name_index: read_u16(data)?,
                descriptor_index: read_u16(data)?,
            },
            1 => {
                let length = read_u16(data)?;
                let bytes_result: Result<Vec<_>, _> =
                    (0..length).into_iter().map(|_| read_u8(data)).collect();
                ConstantPoolInfo::Utf8 {
                    length,
                    string: mutf8::to_string(bytes_result?)?,
                }
            }
            15 => ConstantPoolInfo::MethodHandle {
                reference_kind: read_u8(data)?,
                reference_index: read_u16(data)?,
            },
            16 => ConstantPoolInfo::MethodType {
                descriptor_index: read_u16(data)?,
            },
            18 => ConstantPoolInfo::InvokeDynamic {
                bootstrap_method_attr_index: read_u16(data)?,
                name_and_type_index: read_u16(data)?,
            },
            _ => return Err(ClassFileError::InvalidCPType),
        };
        constant_pool.insert(i, entry);
        i += 1;
        if cp_type == 5 || cp_type == 6 {
            i += 1;
        }
    }
    Ok(ConstantPool {
        data: constant_pool,
    })
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ClassAccessFlags {
    pub acc_public: bool,
    pub acc_final: bool,
    pub acc_super: bool,
    pub acc_interface: bool,
    pub acc_abstract: bool,
    pub acc_synthetic: bool,
    pub acc_annotation: bool,
    pub acc_enum: bool,
}

fn read_class_access_flags<T: Read>(data: &mut T) -> Result<ClassAccessFlags, ClassFileError> {
    let flags = read_u16(data)?;
    Ok(ClassAccessFlags {
        acc_public: flags & 0x0001 > 0,
        acc_final: flags & 0x0010 > 0,
        acc_super: flags & 0x0020 > 0,
        acc_interface: flags & 0x0200 > 0,
        acc_abstract: flags & 0x0400 > 0,
        acc_synthetic: flags & 0x1000 > 0,
        acc_annotation: flags & 0x2000 > 0,
        acc_enum: flags & 0x4000 > 0,
    })
}

fn read_interfaces<T: Read>(data: &mut T) -> Result<Vec<u16>, ClassFileError> {
    let interaces_count = read_u16(data)?;
    let interaces_result: Result<Vec<_>, _> = (0..interaces_count)
        .into_iter()
        .map(|_| read_u16(data))
        .collect();
    Ok(interaces_result?)
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ExceptionTableInfo {
    start_pc: u16,
    end_pc: u16,
    handler_pc: u16,
    catch_type: u16,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum AttributeInfo {
    Raw {
        attribute_name: String,
        info: Vec<u8>,
    },
    ConstantValue {
        constant_value_index: u16,
    },
    Code {
        max_stack: u16,
        max_locals: u16,
        code: Vec<u8>,
        exception_table: Vec<ExceptionTableInfo>,
        attributes: Vec<AttributeInfo>,
    },
    SourceFile {
        sourcefile_index: u16,
    },
}

fn read_attributes<T: Read>(
    data: &mut T,
    constant_pool: &ConstantPool,
) -> Result<Vec<AttributeInfo>, ClassFileError> {
    let attributes_count = read_u16(data)?;
    let mut attributes = Vec::new();

    for _ in 0..attributes_count {
        let attribute_name_index = read_u16(data)?;
        let attribute_length = read_u32(data)?;
        let attribute_name = constant_pool.get_utf8_entry(attribute_name_index)?;

        let attribute = match attribute_name.as_str() {
            "ConstantValue" => AttributeInfo::ConstantValue {
                constant_value_index: read_u16(data)?,
            },
            "SourceFile" => AttributeInfo::SourceFile {
                sourcefile_index: read_u16(data)?,
            },
            "Code" => {
                let max_stack = read_u16(data)?;
                let max_locals = read_u16(data)?;
                let code_length = read_u32(data)?;
                let code_result: Result<Vec<_>, _> = (0..code_length)
                    .into_iter()
                    .map(|_| read_u8(data))
                    .collect();
                let exception_table_length = read_u16(data)?;
                let mut exception_table = Vec::with_capacity(exception_table_length as usize);
                for _ in 0..exception_table_length {
                    exception_table.push(ExceptionTableInfo {
                        start_pc: read_u16(data)?,
                        end_pc: read_u16(data)?,
                        handler_pc: read_u16(data)?,
                        catch_type: read_u16(data)?,
                    });
                }
                let inner_attributes = read_attributes(data, constant_pool)?;
                AttributeInfo::Code {
                    max_stack,
                    max_locals,
                    code: code_result?,
                    exception_table,
                    attributes: inner_attributes,
                }
            }
            _ => {
                let bytes_result: Result<Vec<_>, _> = (0..attribute_length)
                    .into_iter()
                    .map(|_| read_u8(data))
                    .collect();
                AttributeInfo::Raw {
                    attribute_name,
                    info: bytes_result?,
                }
            }
        };
        attributes.push(attribute);
    }
    Ok(attributes)
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FieldAccessFlags {
    pub acc_public: bool,
    pub acc_private: bool,
    pub acc_protected: bool,
    pub acc_static: bool,
    pub acc_final: bool,
    pub acc_volatile: bool,
    pub acc_transient: bool,
    pub acc_synthetic: bool,
    pub acc_enum: bool,
}

fn read_field_access_flags<T: Read>(data: &mut T) -> Result<FieldAccessFlags, ClassFileError> {
    let flags = read_u16(data)?;
    Ok(FieldAccessFlags {
        acc_public: flags & 0x0001 > 0,
        acc_private: flags & 0x0002 > 0,
        acc_protected: flags & 0x0004 > 0,
        acc_static: flags & 0x0008 > 0,
        acc_final: flags & 0x0010 > 0,
        acc_volatile: flags & 0x0040 > 0,
        acc_transient: flags & 0x0080 > 0,
        acc_synthetic: flags & 0x1000 > 0,
        acc_enum: flags & 0x4000 > 0,
    })
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FieldInfo {
    pub access_flags: FieldAccessFlags,
    pub name_index: u16,
    pub descriptor_index: u16,
    pub attributes: Vec<AttributeInfo>,
}

fn read_fields<T: Read>(
    data: &mut T,
    constant_pool: &ConstantPool,
) -> Result<Vec<FieldInfo>, ClassFileError> {
    let fields_count = read_u16(data)?;
    let mut fields = Vec::new();
    for _ in 0..fields_count {
        let access_flags = read_field_access_flags(data)?;
        let name_index = read_u16(data)?;
        let descriptor_index = read_u16(data)?;
        let attributes = read_attributes(data, constant_pool)?;
        let field = FieldInfo {
            access_flags,
            name_index,
            descriptor_index,
            attributes,
        };
        fields.push(field);
    }
    Ok(fields)
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MethodAccessFlags {
    pub acc_public: bool,
    pub acc_private: bool,
    pub acc_protected: bool,
    pub acc_static: bool,
    pub acc_final: bool,
    pub acc_synchronized: bool,
    pub acc_bridge: bool,
    pub acc_varargs: bool,
    pub acc_native: bool,
    pub acc_abstract: bool,
    pub acc_strict: bool,
    pub acc_synthetic: bool,
}

fn read_method_access_flags<T: Read>(data: &mut T) -> Result<MethodAccessFlags, ClassFileError> {
    let flags = read_u16(data)?;
    Ok(MethodAccessFlags {
        acc_public: flags & 0x0001 > 0,
        acc_private: flags & 0x0002 > 0,
        acc_protected: flags & 0x0004 > 0,
        acc_static: flags & 0x0008 > 0,
        acc_final: flags & 0x0010 > 0,
        acc_synchronized: flags & 0x0020 > 0,
        acc_bridge: flags & 0x0040 > 0,
        acc_varargs: flags & 0x0080 > 0,
        acc_native: flags & 0x0100 > 0,
        acc_abstract: flags & 0x0400 > 0,
        acc_strict: flags & 0x0800 > 0,
        acc_synthetic: flags & 0x1000 > 0,
    })
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MethodInfo {
    pub access_flags: MethodAccessFlags,
    pub name_index: u16,
    pub descriptor_index: u16,
    pub attributes: Vec<AttributeInfo>,
}

fn read_methods<T: Read>(
    data: &mut T,
    constant_pool: &ConstantPool,
) -> Result<Vec<MethodInfo>, ClassFileError> {
    let methods_count = read_u16(data)?;
    let mut methods = Vec::new();
    for _ in 0..methods_count {
        let access_flags = read_method_access_flags(data)?;
        let name_index = read_u16(data)?;
        let descriptor_index = read_u16(data)?;
        let attributes = read_attributes(data, constant_pool)?;
        let field = MethodInfo {
            access_flags,
            name_index,
            descriptor_index,
            attributes,
        };
        methods.push(field);
    }
    Ok(methods)
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ClassFile {
    pub major_version: u16,
    pub minor_version: u16,
    pub constant_pool: ConstantPool,
    pub access_flags: ClassAccessFlags,
    pub this_class: u16,
    pub super_class: u16,
    pub interfaces: Vec<u16>,
    pub fields: Vec<FieldInfo>,
    pub methods: Vec<MethodInfo>,
    pub attributes: Vec<AttributeInfo>,
}

pub fn read_classfile<T: Read>(data: &mut T) -> Result<ClassFile, ClassFileError> {
    if read_u32(data)? != 0xcafebabe {
        return Err(ClassFileError::InvalidMagic);
    }
    let minor_version = read_u16(data)?;
    let major_version = read_u16(data)?;

    let constant_pool = read_constant_pool(data)?;

    let access_flags = read_class_access_flags(data)?;

    let this_class = read_u16(data)?;
    let super_class = read_u16(data)?;

    let interfaces = read_interfaces(data)?;
    let fields = read_fields(data, &constant_pool)?;
    let methods = read_methods(data, &constant_pool)?;
    let attributes = read_attributes(data, &constant_pool)?;

    if let Ok(_) = read_u8(data) {
        return Err(ClassFileError::MoreData);
    }

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