metasploit-framework/lib/rex/java/serialization/model/new_enum.rb

79 lines
2.6 KiB
Ruby
Raw Normal View History

2014-12-10 15:52:23 +00:00
# -*- coding: binary -*-
2014-12-03 20:50:03 +00:00
module Rex
module Java
module Serialization
module Model
# This class provides a NewEnum (Java Enum) representation
class NewEnum < Element
2014-12-05 22:05:35 +00:00
include Rex::Java::Serialization::Model::Contents
2014-12-03 20:50:03 +00:00
# @!attribute enum_description
2014-12-10 01:37:42 +00:00
# @return [Rex::Java::Serialization::Model::ClassDescription] The description of the enum
2014-12-03 20:50:03 +00:00
attr_accessor :enum_description
# @!attribute constant_name
2014-12-10 01:37:42 +00:00
# @return [Rex::Java::Serialization::Model::Utf] The constant value in the Java Enum
2014-12-03 20:50:03 +00:00
attr_accessor :constant_name
2014-12-06 02:11:45 +00:00
# @param stream [Rex::Java::Serialization::Model::Stream] the stream where it belongs to
2014-12-05 22:52:59 +00:00
def initialize(stream = nil)
super(stream)
2014-12-03 20:50:03 +00:00
self.enum_description = nil
self.constant_name = nil
end
2014-12-10 01:37:42 +00:00
# Deserializes a Rex::Java::Serialization::Model::NewEnum
2014-12-03 20:50:03 +00:00
#
# @param io [IO] the io to read from
# @return [self] if deserialization succeeds
# @raise [RuntimeError] if deserialization doesn't succeed
def decode(io)
2014-12-05 23:14:37 +00:00
self.enum_description = ClassDesc.decode(io, stream)
2014-12-05 23:35:38 +00:00
stream.add_reference(self) unless stream.nil?
2014-12-03 20:50:03 +00:00
self.constant_name = decode_constant_name(io)
self
end
2014-12-10 01:37:42 +00:00
# Serializes the Rex::Java::Serialization::Model::NewEnum
2014-12-03 20:50:03 +00:00
#
# @return [String] if serialization succeeds
# @raise [RuntimeError] if serialization doesn't succeed
def encode
2014-12-06 02:16:50 +00:00
unless enum_description.class == ClassDesc &&
constant_name.class == Utf
2014-12-03 20:50:03 +00:00
raise ::RuntimeError, 'Failed to serialize EnumDescription'
end
encoded = ''
encoded << enum_description.encode
2014-12-05 22:05:35 +00:00
encoded << encode_content(constant_name)
2014-12-03 20:50:03 +00:00
encoded
end
2014-12-07 23:52:09 +00:00
# Creates a print-friendly string representation
#
# @return [String]
def to_s
constant_name.to_s
end
2014-12-03 20:50:03 +00:00
private
# Deserializes the NewEnum constant name
#
# @param io [IO] the io to read from
# @return [Rex::Java::Serialization::Model::Utf] if deserialization succeeds
# @raise [RuntimeError] if deserialization doesn't succed
def decode_constant_name(io)
2014-12-05 23:14:37 +00:00
content = decode_content(io, stream)
2014-12-05 22:05:35 +00:00
raise ::RuntimeError, 'Failed to unserialize NewEnum' unless content.class == Rex::Java::Serialization::Model::Utf
2014-12-03 20:50:03 +00:00
2014-12-05 22:05:35 +00:00
content
2014-12-03 20:50:03 +00:00
end
end
end
end
end
end