|
| 1 | +#include "Class.h" |
| 2 | + |
| 3 | +#include <sese/Log.h> |
| 4 | +#include <sese/util/Endian.h> |
| 5 | +#include <sese/util/Exception.h> |
| 6 | + |
| 7 | +jvm::Class::Class(const std::string &path) { |
| 8 | + SESE_DEBUG("open %s", path.c_str()); |
| 9 | + file = sese::io::File::create(path, sese::io::File::B_READ); |
| 10 | + if (!file) throw sese::Exception("cannot open file"); |
| 11 | + SESE_DEBUG("open success"); |
| 12 | +} |
| 13 | + |
| 14 | +void jvm::Class::parse() { |
| 15 | + parseMagicNumber(); |
| 16 | + parseVersion(); |
| 17 | + parseConstantPool(); |
| 18 | + parseClass(); |
| 19 | + parseFields(); |
| 20 | + parseMethods(); |
| 21 | + parseAttributes(); |
| 22 | +} |
| 23 | + |
| 24 | +#define IF_READ(m) if (sizeof(m) != file->read(&m, sizeof(m))) |
| 25 | +#define ASSERT_READ(m) IF_READ(m) throw sese::Exception("failed to parse " #m); |
| 26 | + |
| 27 | +void jvm::Class::parseMagicNumber() { |
| 28 | + ASSERT_READ(magic) |
| 29 | + magic = ToBigEndian32(magic); |
| 30 | + SESE_DEBUG("magic number 0x%x", magic); |
| 31 | +} |
| 32 | + |
| 33 | +void jvm::Class::parseVersion() { |
| 34 | + ASSERT_READ(minor) |
| 35 | + minor = FromBigEndian16(minor); |
| 36 | + SESE_DEBUG("minor version %d", minor); |
| 37 | + ASSERT_READ(minor) |
| 38 | + major = FromBigEndian16(major); |
| 39 | + SESE_DEBUG("major version %d", major); |
| 40 | +} |
| 41 | + |
| 42 | +void jvm::Class::parseConstantPool() { |
| 43 | + ASSERT_READ(constant_pool_count) |
| 44 | + constant_pool_count = FromBigEndian16(constant_pool_count); |
| 45 | + SESE_DEBUG("constant pool count %d", constant_pool_count); |
| 46 | + |
| 47 | + for (int i = 1; i < constant_pool_count; i++) { |
| 48 | + int8_t tag; |
| 49 | + ASSERT_READ(tag) |
| 50 | + SESE_DEBUG("tag %d", tag); |
| 51 | + if (tag == utf8_info) { |
| 52 | + int16_t length; |
| 53 | + char bytes[UINT16_MAX]{}; |
| 54 | + ASSERT_READ(length) |
| 55 | + length = FromBigEndian16(length); |
| 56 | + SESE_DEBUG("length %d", length); |
| 57 | + if (length != file->read(bytes, length)) { |
| 58 | + throw sese::Exception("failed to parse bytes"); |
| 59 | + } |
| 60 | + SESE_DEBUG("bytes %s", bytes); |
| 61 | + } else if (tag == class_info) { |
| 62 | + int16_t index; |
| 63 | + ASSERT_READ(index) |
| 64 | + index = FromBigEndian16(index); |
| 65 | + SESE_DEBUG("index %d", index); |
| 66 | + } else if (tag == method_ref_info) { |
| 67 | + int16_t index; |
| 68 | + ASSERT_READ(index) |
| 69 | + index = FromBigEndian16(index); |
| 70 | + SESE_DEBUG("index %d", index); |
| 71 | + ASSERT_READ(index) |
| 72 | + index = FromBigEndian16(index); |
| 73 | + SESE_DEBUG("index %d", index); |
| 74 | + } else if (tag == name_and_type_info) { |
| 75 | + int16_t index; |
| 76 | + ASSERT_READ(index) |
| 77 | + index = FromBigEndian16(index); |
| 78 | + SESE_DEBUG("index %d", index); |
| 79 | + ASSERT_READ(index) |
| 80 | + index = FromBigEndian16(index); |
| 81 | + SESE_DEBUG("index %d", index); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void jvm::Class::parseClass() { |
| 87 | + ASSERT_READ(access_flags) |
| 88 | + access_flags = FromBigEndian16(access_flags); |
| 89 | + SESE_DEBUG("access flags 0x%x", access_flags); |
| 90 | + |
| 91 | + ASSERT_READ(this_class) |
| 92 | + this_class = FromBigEndian16(this_class); |
| 93 | + SESE_DEBUG("this class %d", this_class); |
| 94 | + |
| 95 | + ASSERT_READ(super_class) |
| 96 | + super_class = FromBigEndian16(super_class); |
| 97 | + SESE_DEBUG("super class %d", super_class); |
| 98 | + |
| 99 | + ASSERT_READ(interface_count) |
| 100 | + interface_count = FromBigEndian16(interface_count); |
| 101 | + SESE_DEBUG("interface count %d", interface_count); |
| 102 | + |
| 103 | + interfaces.reserve(interface_count); |
| 104 | + for (int i = 0; i < interface_count; i++) { |
| 105 | + uint16_t iface; |
| 106 | + ASSERT_READ(iface) |
| 107 | + iface = FromBigEndian16(iface); |
| 108 | + SESE_DEBUG("interface %d", iface); |
| 109 | + interfaces.push_back(iface); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void jvm::Class::parseFields() { |
| 114 | + ASSERT_READ(fields_count) |
| 115 | + fields_count = FromBigEndian16(fields_count); |
| 116 | + SESE_DEBUG("fields count %d", fields_count); |
| 117 | + |
| 118 | + field_infos.reserve(fields_count); |
| 119 | + for (int i = 0; i < fields_count; ++i) { |
| 120 | + FieldInfo field_info; |
| 121 | + ASSERT_READ(field_info.access_flags) |
| 122 | + field_info.access_flags = FromBigEndian16(field_info.access_flags); |
| 123 | + ASSERT_READ(field_info.name_index) |
| 124 | + field_info.name_index = FromBigEndian16(field_info.name_index); |
| 125 | + ASSERT_READ(field_info.descriptor_index) |
| 126 | + field_info.descriptor_index = FromBigEndian16(field_info.descriptor_index); |
| 127 | + ASSERT_READ(field_info.attributes_count) |
| 128 | + field_info.attributes_count = FromBigEndian16(field_info.attributes_count); |
| 129 | + field_info.attribute_infos.reserve(field_info.attributes_count); |
| 130 | + for (int j = 0; i < field_info.attributes_count; j++) { |
| 131 | + AttributeInfo attribute_info; |
| 132 | + ASSERT_READ(attribute_info.name_index) |
| 133 | + ASSERT_READ(attribute_info.length) |
| 134 | + attribute_info.length = FromBigEndian32(attribute_info.length); |
| 135 | + attribute_info.info.reserve(attribute_info.length); |
| 136 | + for (int k = 0; k < attribute_info.length; k++) { |
| 137 | + uint8_t byte; |
| 138 | + ASSERT_READ(byte) |
| 139 | + attribute_info.info.push_back(byte); |
| 140 | + } |
| 141 | + field_info.attribute_infos.push_back(attribute_info); |
| 142 | + } |
| 143 | + field_infos.push_back(field_info); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +void jvm::Class::parseMethods() { |
| 148 | + ASSERT_READ(methods_count) |
| 149 | + methods_count = FromBigEndian16(methods_count); |
| 150 | + SESE_DEBUG("methods count %d", methods_count); |
| 151 | + |
| 152 | + for (int i = 0; i< methods_count; ++i) { |
| 153 | + MethodInfo method_info; |
| 154 | + ASSERT_READ(method_info.access_flags) |
| 155 | + method_info.access_flags = FromBigEndian16(method_info.access_flags); |
| 156 | + ASSERT_READ(method_info.name_index) |
| 157 | + method_info.name_index = FromBigEndian16(method_info.name_index); |
| 158 | + ASSERT_READ(method_info.descriptor_index) |
| 159 | + method_info.descriptor_index = FromBigEndian16(method_info.descriptor_index); |
| 160 | + ASSERT_READ(method_info.attributes_count) |
| 161 | + method_info.attributes_count = FromBigEndian16(method_info.attributes_count); |
| 162 | + method_info.attribute_infos.reserve(method_info.attributes_count); |
| 163 | + for (int j = 0; j < method_info.attributes_count; ++j) { |
| 164 | + AttributeInfo attribute_info; |
| 165 | + ASSERT_READ(attribute_info.name_index) |
| 166 | + ASSERT_READ(attribute_info.length) |
| 167 | + attribute_info.length = FromBigEndian32(attribute_info.length); |
| 168 | + attribute_info.info.reserve(attribute_info.length); |
| 169 | + for (int k = 0; k < attribute_info.length; k++) { |
| 170 | + uint8_t byte; |
| 171 | + ASSERT_READ(byte) |
| 172 | + attribute_info.info.push_back(byte); |
| 173 | + } |
| 174 | + method_info.attribute_infos.push_back(attribute_info); |
| 175 | + } |
| 176 | + method_infos.push_back(method_info); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +void jvm::Class::parseAttributes() { |
| 181 | + ASSERT_READ(attributes_count) |
| 182 | + attributes_count = FromBigEndian16(attributes_count); |
| 183 | + SESE_DEBUG("attributes count %d", attributes_count); |
| 184 | + attribute_infos.reserve(attributes_count); |
| 185 | + for (int j = 0; j < attributes_count; ++j) { |
| 186 | + AttributeInfo attribute_info; |
| 187 | + ASSERT_READ(attribute_info.name_index) |
| 188 | + ASSERT_READ(attribute_info.length) |
| 189 | + attribute_info.length = FromBigEndian32(attribute_info.length); |
| 190 | + attribute_info.info.reserve(attribute_info.length); |
| 191 | + for (int k = 0; k < attribute_info.length; k++) { |
| 192 | + uint8_t byte; |
| 193 | + ASSERT_READ(byte) |
| 194 | + attribute_info.info.push_back(byte); |
| 195 | + } |
| 196 | + attribute_infos.push_back(attribute_info); |
| 197 | + } |
| 198 | +} |
| 199 | + |
0 commit comments