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
use std::io;
use std::io::Write;

use attribute::*;
use class_access::*;
use class_file::ClassFile;
use constant_pool::*;
use method::*;

const MAGIC: u32 = 0xCAFE_BABE;

const CONSTANT_TAG_UTF8: u8 = 1;
const CONSTANT_TAG_CLASS: u8 = 7;
const CONSTANT_TAG_FIELDREF: u8 = 9;
const CONSTANT_TAG_METHODREF: u8 = 10;
const CONSTANT_TAG_NAME_AND_TYPE: u8 = 12;

pub fn write_class_file<W: Write>(file: &mut W, class_file: &ClassFile) -> io::Result<()> {
    write_u32(file, MAGIC)?;

    write_u16(file, class_file.minor_version)?;
    write_u16(file, class_file.major_version)?;

    write_constant_pool(file, &class_file.constant_pool)?;

    write_u16(file, ClassAccess::to_access_flags(&class_file.access_flags))?;
    write_u16(file, class_file.this_class)?;
    write_u16(file, class_file.super_class)?;

    write_u16(file, 0)?; // interfaces
    write_u16(file, 0)?; // fields
    write_methods(file, &class_file.methods)?;
    write_attributes(file, &class_file.attributes)?;

    Ok(())
}

fn write_u8<W: Write>(file: &mut W, value: u8) -> io::Result<()> {
    file.write_all(&u8::to_be_bytes(value))
}

fn write_u16<W: Write>(file: &mut W, value: u16) -> io::Result<()> {
    file.write_all(&u16::to_be_bytes(value))
}

fn write_u32<W: Write>(file: &mut W, value: u32) -> io::Result<()> {
    file.write_all(&u32::to_be_bytes(value))
}

fn write_n_bytes<W: Write>(file: &mut W, bytes: &[u8]) -> io::Result<()> {
    file.write_all(bytes)
}

fn write_constant_pool<W: Write>(file: &mut W, constant_pool: &[Box<ConstantPoolEntry>]) -> io::Result<()> {
    write_u16(file, (constant_pool.len() + 1) as u16)?;

    for entry in constant_pool {
        write_constant_pool_entry(file, entry)?;
    }

    Ok(())
}

#[allow(clippy::borrowed_box)]
fn write_constant_pool_entry<W: Write>(file: &mut W, entry: &Box<ConstantPoolEntry>) -> io::Result<()> {
    use ConstantPoolEntry::*;

    match **entry {
        ConstantUtf8 { ref string } => write_constant_utf8(file, &string)?,
        ConstantClass { name_index } => write_constant_class(file, name_index)?,
        ConstantMethodref { class_index, name_and_type_index } =>
            write_constant_methodref(file, class_index, name_and_type_index)?,
        ConstantNameAndType { name_index, descriptor_index } =>
            write_constant_name_and_type(file, name_index, descriptor_index)?,
        _ => panic!(),
    }

    Ok(())
}

fn write_constant_utf8<W: Write>(file: &mut W, string: &str) -> io::Result<()> {
    let bytes = string.as_bytes();

    write_u8(file, CONSTANT_TAG_UTF8)?;
    write_u16(file, bytes.len() as u16)?;
    write_n_bytes(file, &bytes)?;

    Ok(())
}

fn write_constant_class<W: Write>(file: &mut W, name_index: u16) -> io::Result<()> {
    write_u8(file, CONSTANT_TAG_CLASS)?;
    write_u16(file, name_index)?;

    Ok(())
}

fn write_constant_methodref<W: Write>(file: &mut W, class_index: u16, name_and_type_index: u16) -> io::Result<()> {
    write_u8(file, CONSTANT_TAG_METHODREF)?;
    write_u16(file, class_index)?;
    write_u16(file, name_and_type_index)?;

    Ok(())
}

fn write_constant_name_and_type<W: Write>(file: &mut W, name_index: u16, descriptor_index: u16) -> io::Result<()> {
    write_u8(file, CONSTANT_TAG_NAME_AND_TYPE)?;
    write_u16(file, name_index)?;
    write_u16(file, descriptor_index)?;

    Ok(())
}

fn write_methods<W: Write>(file: &mut W, methods: &[Method]) -> io::Result<()> {
    write_u16(file, methods.len() as u16)?;

    for method in methods.iter() {
        write_method(file, method)?;
    }

    Ok(())
}

fn write_method<W: Write>(file: &mut W, method: &Method) -> io::Result<()> {
    write_u16(file, method.access_flags)?;
    write_u16(file, method.name_index)?;
    write_u16(file, method.descriptor_index)?;

    write_attributes(file, &method.attributes)?;

    Ok(())
}

fn write_attributes<W: Write>(file: &mut W, attributes: &[Attribute]) -> io::Result<()> {
    write_u16(file, attributes.len() as u16)?;

    for attribute in attributes.iter() {
        write_attribute(file, attribute)?;
    }

    Ok(())
}

fn write_attribute<W: Write>(file: &mut W, attributes: &Attribute) -> io::Result<()> {
    write_u16(file, attributes.attribute_name_index)?;
    write_u32(file, attributes.info.len() as u32)?;
    write_n_bytes(file, &attributes.info)?;

    Ok(())
}