2014-12-10 09:52:23 -06:00
|
|
|
# -*- coding: binary -*-
|
|
|
|
|
|
2014-12-01 19:07:45 -06:00
|
|
|
module Rex
|
|
|
|
|
module Java
|
|
|
|
|
module Serialization
|
|
|
|
|
module Model
|
|
|
|
|
# This class provides a field description representation (fieldDesc). It's used for
|
|
|
|
|
# both primitive descriptions (primitiveDesc) and object descriptions (objectDesc).
|
|
|
|
|
class Field < Element
|
|
|
|
|
|
2014-12-05 16:05:35 -06:00
|
|
|
include Rex::Java::Serialization::Model::Contents
|
2014-12-01 19:07:45 -06:00
|
|
|
|
|
|
|
|
# @!attribute type
|
2015-04-03 14:01:31 -05:00
|
|
|
# @return [String] The type of the field.
|
2014-12-01 19:07:45 -06:00
|
|
|
attr_accessor :type
|
|
|
|
|
# @!attribute name
|
2015-04-03 14:01:31 -05:00
|
|
|
# @return [Rex::Java::Serialization::Model::Utf] The name of the field.
|
2014-12-01 19:07:45 -06:00
|
|
|
attr_accessor :name
|
|
|
|
|
# @!attribute field_type
|
2015-04-03 14:01:31 -05:00
|
|
|
# @return [Rex::Java::Serialization::Model::Utf] The type of the field on object types.
|
2014-12-01 19:07:45 -06:00
|
|
|
attr_accessor :field_type
|
|
|
|
|
|
2014-12-05 20:11:45 -06:00
|
|
|
# @param stream [Rex::Java::Serialization::Model::Stream] the stream where it belongs to
|
2014-12-05 16:52:59 -06:00
|
|
|
def initialize(stream = nil)
|
|
|
|
|
super(stream)
|
2014-12-01 19:07:45 -06:00
|
|
|
self.type = ''
|
|
|
|
|
self.name = nil
|
|
|
|
|
self.field_type = nil
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-09 19:37:42 -06:00
|
|
|
# Deserializes a Rex::Java::Serialization::Model::Field
|
2014-12-01 19:07:45 -06:00
|
|
|
#
|
|
|
|
|
# @param io [IO] the io to read from
|
2014-12-03 14:59:38 -06:00
|
|
|
# @return [self] if deserialization succeeds
|
2015-04-05 18:43:03 -05:00
|
|
|
# @raise [Rex::Java::Serialization::DecodeError] if deserialization doesn't succeed
|
2014-12-01 19:07:45 -06:00
|
|
|
def decode(io)
|
|
|
|
|
code = io.read(1)
|
|
|
|
|
|
2014-12-02 15:31:31 -06:00
|
|
|
unless code && is_valid?(code)
|
2015-04-05 18:43:03 -05:00
|
|
|
raise Rex::Java::Serialization::DecodeError, 'Failed to unserialize Field'
|
2014-12-02 15:31:31 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
self.type = TYPE_CODES[code]
|
2014-12-05 17:14:37 -06:00
|
|
|
self.name = Utf.decode(io, stream)
|
2014-12-01 19:07:45 -06:00
|
|
|
|
|
|
|
|
if is_object?
|
|
|
|
|
self.field_type = decode_field_type(io)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-09 19:37:42 -06:00
|
|
|
# Serializes the Rex::Java::Serialization::Model::Field
|
2014-12-01 19:07:45 -06:00
|
|
|
#
|
2014-12-03 14:59:38 -06:00
|
|
|
# @return [String] if serialization succeeds
|
2015-04-05 18:43:03 -05:00
|
|
|
# @raise [Rex::Java::Serialization::EncodeError] if serialization doesn't succeed
|
2014-12-01 19:07:45 -06:00
|
|
|
def encode
|
2015-01-28 17:00:15 -06:00
|
|
|
unless name.kind_of?(Rex::Java::Serialization::Model::Utf)
|
2015-04-05 18:43:03 -05:00
|
|
|
raise Rex::Java::Serialization::EncodeError, 'Failed to serialize Field'
|
2014-12-03 15:06:28 -06:00
|
|
|
end
|
|
|
|
|
|
2014-12-01 19:07:45 -06:00
|
|
|
unless is_type_valid?
|
2015-04-05 18:43:03 -05:00
|
|
|
raise Rex::Java::Serialization::EncodeError, 'Failed to serialize Field'
|
2014-12-01 19:07:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
encoded = ''
|
|
|
|
|
encoded << TYPE_CODES.key(type)
|
|
|
|
|
encoded << name.encode
|
|
|
|
|
|
|
|
|
|
if is_object?
|
2014-12-05 12:42:27 -06:00
|
|
|
encoded << encode_field_type
|
2014-12-01 19:07:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
encoded
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Whether the field type is valid.
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
def is_type_valid?
|
|
|
|
|
if TYPE_CODES.values.include?(type)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Whether the field type is a primitive one.
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
def is_primitive?
|
|
|
|
|
if PRIMITIVE_TYPE_CODES.values.include?(type)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Whether the field type is an object one.
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
def is_object?
|
|
|
|
|
if OBJECT_TYPE_CODES.values.include?(type)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-07 17:52:09 -06:00
|
|
|
# Creates a print-friendly string representation
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
|
|
|
|
def to_s
|
|
|
|
|
str = "#{name} "
|
|
|
|
|
if is_primitive?
|
|
|
|
|
str << "(#{type})"
|
|
|
|
|
else
|
|
|
|
|
str << "(#{field_type})"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
str
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-01 19:07:45 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
|
|
# Whether the type opcode is a valid one.
|
|
|
|
|
#
|
|
|
|
|
# @param code [String] A type opcode
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
def is_valid?(code)
|
|
|
|
|
if TYPE_CODES.keys.include?(code)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Serializes the `field_type` attribute.
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
2015-04-05 18:43:03 -05:00
|
|
|
# @raise [Rex::Java::Serialization::EncodeError] if serialization fails
|
2014-12-01 19:07:45 -06:00
|
|
|
def encode_field_type
|
2014-12-05 19:55:52 -06:00
|
|
|
allowed_contents = [Utf, Reference]
|
|
|
|
|
|
|
|
|
|
unless allowed_contents.include?(field_type.class)
|
2015-04-05 18:43:03 -05:00
|
|
|
raise Rex::Java::Serialization::EncodeError, 'Failed to serialize Field'
|
2014-12-03 15:06:28 -06:00
|
|
|
end
|
|
|
|
|
|
2014-12-05 16:05:35 -06:00
|
|
|
encoded = encode_content(field_type)
|
2014-12-01 19:07:45 -06:00
|
|
|
|
|
|
|
|
encoded
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-03 14:59:38 -06:00
|
|
|
# Deserializes the `field_type` value.
|
2014-12-01 19:07:45 -06:00
|
|
|
#
|
|
|
|
|
# @param io [IO] the io to read from
|
|
|
|
|
# @return [Java::Serialization::Model::Utf]
|
2015-04-05 18:43:03 -05:00
|
|
|
# @raise [Rex::Java::Serialization::DecodeError] if unserialization doesn't succeed
|
2014-12-01 19:07:45 -06:00
|
|
|
def decode_field_type(io)
|
2014-12-05 19:55:52 -06:00
|
|
|
allowed_contents = [Utf, Reference]
|
2014-12-05 17:14:37 -06:00
|
|
|
type = decode_content(io, stream)
|
2014-12-05 16:05:35 -06:00
|
|
|
|
2014-12-05 19:55:52 -06:00
|
|
|
unless allowed_contents.include?(type.class)
|
2015-04-05 18:43:03 -05:00
|
|
|
raise Rex::Java::Serialization::DecodeError, 'Failed to unserialize Field field_type'
|
2014-12-02 15:31:31 -06:00
|
|
|
end
|
2014-12-01 19:07:45 -06:00
|
|
|
|
|
|
|
|
type
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-28 17:00:15 -06:00
|
|
|
end
|