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
use nom::{
  be_u16, be_u32,
  IResult,
};

use attribute_info::{   AttributeInfo,
                        CodeAttribute,
                        ExceptionEntry,
                        ExceptionsAttribute,
                        ConstantValueAttribute
                        };

pub fn attribute_parser(input: &[u8]) -> IResult<&[u8], AttributeInfo> {
    do_parse!(input,
        attribute_name_index: be_u16 >>
        attribute_length: be_u32 >>
        info: take!(attribute_length)>>
        (AttributeInfo {
            attribute_name_index: attribute_name_index,
            attribute_length: attribute_length,
            info: info.to_owned(),
        })
    )
}

pub fn exception_entry_parser(input: &[u8]) -> IResult<&[u8], ExceptionEntry> {
    do_parse!(input,
        start_pc: be_u16 >>
        end_pc: be_u16 >>
        handler_pc: be_u16 >>
        catch_type: be_u16>>
        (ExceptionEntry {
            start_pc: start_pc,
            end_pc: end_pc,
            handler_pc: handler_pc,
            catch_type: catch_type,
        })
    )
}

pub fn code_attribute_parser(input: &[u8]) -> IResult<&[u8], CodeAttribute> {
    do_parse!(input,
        max_stack: be_u16 >>
        max_locals: be_u16 >>
        code_length: be_u32 >>
        code: take!(code_length) >>
        exception_table_length: be_u16 >>
        exception_table: count!(exception_entry_parser, exception_table_length as usize) >>
        attributes_count: be_u16 >>
        attributes: count!(attribute_parser, attributes_count as usize) >>
        (CodeAttribute {
            max_stack: max_stack,
            max_locals: max_locals,
            code_length: code_length,
            code: code.to_owned(),
            exception_table_length: exception_table_length,
            exception_table: exception_table,
            attributes_count: attributes_count,
            attributes: attributes,
        })
    )
}

pub fn exceptions_attribute_parser(input: &[u8]) -> IResult<&[u8], ExceptionsAttribute> {
    do_parse!(input,
        exception_table_length: be_u16 >>
        exception_table: count!(be_u16, exception_table_length as usize) >>
        (ExceptionsAttribute {
        exception_table_length: exception_table_length,
        exception_table: exception_table,
        })
    )
}

pub fn constant_value_attribute_parser(input: &[u8]) -> IResult<&[u8], ConstantValueAttribute> {
    do_parse!(input,
        constant_value_index: be_u16 >>
        (ConstantValueAttribute {
            constant_value_index: constant_value_index,
        })
    )
}