def parse(self, fd, class_file): self.access_flags = Method.AccessFlags() self.access_flags.parse(fd) self.name_index = read_bytes.read_u2_int(fd) self.descriptor_index = read_bytes.read_u2_int(fd) self.attributes_count, self.attributes = attributes.parse( fd, class_file)
def parse(self, fd): self.offset_delta = read_bytes.read_u2_int(fd) self.number_of_locals = read_bytes.read_u2_int(fd) for _ in range(self.number_of_locals): v_type_info = parse_verification_type_info(fd) self.locals.append(v_type_info) self.number_of_stack_items = read_bytes.read_u2_int(fd) for _ in range(self.number_of_stack_items): v_type_info = parse_verification_type_info(fd) self.stack.append(v_type_info)
def parse_verification_type_info(fd): tag = read_bytes.read_u1_int(fd) if tag in (ITEM_Top, ITEM_Integer, ITEM_Float, ITEM_Null, ITEM_UninitializedThis, ITEM_Long, ITEM_Double): return (tag, None) if tag == ITEM_Object: cpool_index = read_bytes.read_u2_int(fd) return (tag, cpool_index) if tag == ITEM_Uninitialized: offset = read_bytes.read_u2_int(fd) return (tag, offset) raise ValueError( 'Invalid verification_type_info tag value {0}'.format(tag))
def parse(self, fd) -> ClassStruct: class_struct = ClassStruct() class_struct.magic = read_bytes.read_u4_int(fd) assert class_struct.magic == 0xCAFEBABE, 'Magic number ({0}) in class file is wrong'.format( class_struct.magic) class_struct.minor_version = read_bytes.read_u2_int(fd) class_struct.major_version = read_bytes.read_u2_int(fd) class_struct.constant_pool = constant_pool.parse(fd) class_struct.access_flags = AccessFlags() class_struct.access_flags.parse(fd) class_struct.this_class = read_bytes.read_u2_int(fd) class_struct.super_class = read_bytes.read_u2_int(fd) class_struct.interfaces_count = read_bytes.read_u2_int(fd) for _ in range(class_struct.interfaces_count): class_struct.interfaces.append(read_bytes.read_u2_int(fd)) class_struct.fields_count = read_bytes.read_u2_int(fd) for _ in range(class_struct.fields_count): field = Field() field.parse(fd, class_struct) class_struct.fields.append(field) class_struct.methods_count = read_bytes.read_u2_int(fd) for _ in range(class_struct.methods_count): method = Method() method.parse(fd, class_struct) class_struct.methods.append(method) (class_struct.attributes_count, class_struct.attributes) = attributes.parse(fd, class_struct) assert len( fd.read(1) ) == 0, 'Class file is finish parsed, but still data left in tail.' class_struct.validate() return class_struct
def parse(fd, class_file): '''Parse attributes ''' count = read_bytes.read_u2_int(fd) attributes = [] for _ in range(count): attr = parse_attr(fd, class_file) attributes.append(attr) return (count, attributes)
def parse_attr(fd, class_file): name_index = read_bytes.read_u2_int(fd) length = read_bytes.read_u4_int(fd) name_constant = class_file.constant_pool[name_index] assert type( name_constant ) == constant_pool.ConstantUtf8, 'Attribute name constant is not CONSTANT_Utf8_info.' attribute_type = { 'ConstantValue': ConstantValueAttribute, 'Code': CodeAttribute, 'StackMapTable': StackMapTableAttribute, 'Exceptions': ExceptionsAttribute, 'BootstrapMethods': BootstrapMethodsAttribute }.get(name_constant.value(), Attribute) attr = attribute_type(name_constant.value(), length) attr.parse_info(fd, class_file) return attr
def parse_info(self, fd, class_file): self.max_stack = read_bytes.read_u2_int(fd) self.max_locals = read_bytes.read_u2_int(fd) self.code_length = read_bytes.read_u4_int(fd) self.code = fd.read(self.code_length) self.exception_table_length = read_bytes.read_u2_int(fd) self.exception_table = [] for _ in range(self.exception_table_length): start_pc = read_bytes.read_u2_int(fd) end_pc = read_bytes.read_u2_int(fd) handler_pc = read_bytes.read_u2_int(fd) catch_type = read_bytes.read_u2_int(fd) self.exception_table.append( tuple(start_pc, end_pc, handler_pc, catch_type)) (self.attributes_count, self.attributes) = parse(fd, class_file) self.code_to_instructions()
def parse(self, fd): self._flags = read_bytes.read_u2_int(fd)
def parse(self, fd): self.offset_delta = read_bytes.read_u2_int(fd) for _ in range(self.num_of_additional): v_type_info = parse_verification_type_info(fd) self.locals.append(v_type_info)
def parse(self, fd): self.offset_delta = read_bytes.read_u2_int(fd)
def parse(self, fd): self.offset_delta = read_bytes.read_u2_int(fd) self.verification_type_info = parse_verification_type_info(fd)
def parse_info(self, fd, class_file): self.number_of_entries = read_bytes.read_u2_int(fd) self.stack_map_frame_entries = [] for _ in range(self.number_of_entries): frame = parse_stack_map_frame(fd) self.stack_map_frame_entries.append(frame)